can't create DataFrame with multiIndex by list of list · Issue #32173 · pandas-dev/pandas (original) (raw)

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