1

I have python string list, which i want to convert into pandas dataframe with predefined columns. I have tried following code but it shows error.

I have tried following code.

  import pandas as pd
  list = ['jack', '9860', 'datasc', 'vill','0', 'stack']
  df = pd.DataFrame(list, columns= ['name', 'no','job'])

ValueError: Shape of passed values is (1, 6), indices imply (3, 6)

0

1 Answer 1

2

Dont use variable list, because python code word (builtin).

Convert list to numpy array and reshape:

L = ['jack', '9860', 'datasc', 'vill','0', 'stack']
df = pd.DataFrame(np.array(L).reshape(-1,3), columns= ['name', 'no','job'])
print (df)
   name    no     job
0  jack  9860  datasc
1  vill     0   stack
Sign up to request clarification or add additional context in comments.

3 Comments

what is mean reshape(-1,3)
@Coder - -1 means count number of N in shape - (Nx3) - it means it create 3 columns and counted number of rows by number of values in list.
@Coder - similar solution in last paragraph in example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.