4

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
2
  • This error seems pretty clear cut, no? You are passing only 3 column names, but there are 100 columns in the data. Commented Jan 26, 2017 at 17:51
  • @juanpa.arrivillaga - Yes, I understand the error. I am just not able to find a way to make that setting to convert it into a DF Commented Jan 26, 2017 at 17:56

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.