简体   繁体   中英

Python list of lists to dataframe - AssertionError

I have a list of lists in python. I am trying to convert it into a dataframe. For eg =

foo = [
    [1,2,3...],
    [a,b,c...],
    [aa,bb,cc...]
]

Each of these 3 lists have 100 elements in them. I have tried the following to convert to a dataframe -

df = pandas.DataFrame(foo, columns=headers)  // where headers is ['id', 'fname', 'lname']
df = pandas.DataFrame(foo, columns=[foo[0], foo[1], foo[2]])

However I am getting this error -

AssertionError: 3 columns passed, passed data had 100 columns

You can try the following methods. The error comes from the fact that each sublist is interpreted as a row when using pandas.DataFrame constructor. You can either make a dictionary out of the headers and the list:

import pandas as pd
headers = ['id', 'fname', 'name']
df = pd.DataFrame(dict(zip(headers, foo)))

df
#fname  id  lname
#0   a   1     aa
#1   b   2     bb
#2   c   3     cc
#3   d   4     dd
#4   e   5     ee

Or transpose the list:

df = pd.DataFrame(list(zip(*foo)), columns=headers)

df
#  id   fname   lname
#0  1       a      aa
#1  2       b      bb
#2  3       c      cc
#3  4       d      dd
#4  5       e      ee

You can also try DataFrame.from_records transposing the Dataframe:

In [17]: df = pd.DataFrame.from_records(foo).T

In [18]: df
Out[18]: 
   0  1   2
0  1  a  aa
1  2  b  bb
2  3  c  cc

In [19]: df.columns = ['id', 'fname', 'lname']

In [20]: df
Out[20]: 
  id fname lname
0  1     a    aa
1  2     b    bb
2  3     c    cc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM