Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 707
newbie Avatar asked Feb 05 '23 05:02

newbie


1 Answers

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
like image 61
Psidom Avatar answered Feb 08 '23 08:02

Psidom