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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With