2

I have a 3d list

l=[ [ [1,"ab",119.0] ] ,  [ [2,"cd",129.0] ] ,  [ [ 3,"ef",139.0] ] ]

Code

 import pandas as pd , numpy as np
 l=[ [ [1,"ab",119.0] ] , [ [2,"cd",129.0] ] , [ [ 3,"ef",139.0] ] ]
 b=np.array(l)
 print(pd.DataFrame(l))
 print(pd.DataFrame(b))

Conversion of this list to data frame is possible . But when I convert this list to array and then convert the array to data frame I'm getting value Error -Must pass 2d input . Why is this error is generated in case of numpy array and not in list ???

0

1 Answer 1

2

When you pass l to the pd.DataFrame constructor, it reads l as a list of objects, and thus returns a dataframe with one column consisting of the lists inside l:

                0
 0  [1, ab, 119.0]
 1  [2, cd, 129.0]
 2  [3, ef, 139.0]

When you convert l to a np.array, you get a 3d array (3, 1, 1):

[
  [
    ['1' 'ab' '119.0']
  ],
  [
    ['2' 'cd' '129.0']
  ],
  [
    ['3' 'ef' '139.0']
  ]
]

pandas cannot convert this 3d array into a 2d dataframe, thus the error.

If you actually type in your list like this

l = [[1, "ab", 119.0], [2, "cd", 129.0], [3, "ef", 139.0]]

you should have no problem passing in either the list or the array. You would get this dataframe:

   0   1      2
0  1  ab  119.0
1  2  cd  129.0
2  3  ef  139.0
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.