Skip to content

can't create DataFrame with multiIndex by list of list #32173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
redHairGhost opened this issue Feb 22, 2020 · 2 comments · Fixed by #32202
Closed

can't create DataFrame with multiIndex by list of list #32173

redHairGhost opened this issue Feb 22, 2020 · 2 comments · Fixed by #32202
Assignees
Labels
Bug Index Related to the Index class or subclasses MultiIndex
Milestone

Comments

@redHairGhost
Copy link

redHairGhost commented Feb 22, 2020

Code Sample, a copy-pastable example if possible

df=DataFrame([1,2,3,4],[4,5,6,7], columns= [list('abcd'), list('cdef')])

Problem description

when creating DataFrame by list of list with multiIndex, raise ValueError. I'm new to pandas, I don't know why, but it seems like a bug...

d=DataFrame([[1,2,3,4],[4,5,6,7]], columns= [list('abcd'), list('cdef')])

Traceback (most recent call `last):

 File "<ipython-input-70-b1022cf50f33>", line 1, in <module>
    d=DataFrame([[1,2,3,4],[4,5,6,7]], columns= [list('abcd'), list('cdef')])

  File "D:\Users\tangliu\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 450, in __init__
    arrays, columns = to_arrays(data, columns, dtype=dtype)

  File "D:\Users\tangliu\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\construction.py", line 464, in to_arrays
    return _list_to_arrays(data, columns, coerce_float=coerce_float, dtype=dtype)

  File "D:\Users\tangliu\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals\construction.py", line 504, in _list_to_arrays
    raise ValueError(e) from e

ValueError: 2 columns passed, passed data had 4 columns`
It's ok using numpy ndarray or something else
d=DataFrame([[1,2,3,4],[4,5,6,7]])

d

Out[72]: 
   0  1  2  3
0  1  2  3  4
1  4  5  6  7

d.columns=[list('abcd'),list('cdef')]

d
Out[74]: 
   a  b  c  d
   c  d  e  f
0  1  2  3  4
1  4  5  6  7

it's ok when using numpy's ndarray

d=DataFrame(np.random.randn(6,4), columns=[list('abcd'), list('cdef')])

d
Out[81]: 
          a         b         c         d
          c         d         e         f
0 -0.066216 -1.264157 -1.199212 -0.184394
1  0.413666 -0.310750  1.138304 -0.271516
2  0.263854  0.827858  0.652344 -0.024369
3  0.041060 -2.235779  0.707691  0.319255
4 -0.118407 -1.278394  0.398972  0.597036
5  0.048947  0.697497  2.168712  0.650022
@charlesdong1991
Copy link
Member

interesting, looks buggy!

@charlesdong1991
Copy link
Member

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Index Related to the Index class or subclasses MultiIndex
Projects
None yet
3 participants