1

is it possible to do the following (dict is data for the first row index):

dict={'col1':1,'col2':[1,2],'col3':'str'}

nm=pd.DataFrame(dict,index=['new line'])

I expect to receive a data frame looking like:

nm

          col1   col2 col3

new line     1  [1,2]  str

with the values in col2 of type list, but instead I receive the following error:

ValueError: could not broadcast input array from shape (2) into shape (1)

Thanks!

1 Answer 1

2

You are miss one []:

dict={'col1':1,'col2':[[1,2]],'col3':'str'} 
nm=pd.DataFrame(dict,index=['new line'])

print nm
          col1    col2 col3
new line     1  [1, 2]  str

Or:

dict={'col1':[1],'col2':[[1,2]],'col3':['str']} 
nm=pd.DataFrame(dict,index=['new line'])

print nm
          col1    col2 col3
new line     1  [1, 2]  str

If you have more values, you need use list to each column as Series, so if you need list in column, you have to use list of lists:

dict={'col1':[1, 2],'col2':[[1,2], [3,5]],'col3':['str', 'str1']} 
nm=pd.DataFrame(dict,index=['new line', 'new line1'])

print nm
           col1    col2  col3
new line      1  [1, 2]   str
new line1     2  [3, 5]  str1

Storing non-scalar values as data elements is ill-advised and typically you lose access to vectorised methods as np and pandas has no vectorised methods for appending to a list in a vectorised manner. link

Sign up to request clarification or add additional context in comments.

Comments

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.