CreamStat
CreamStat

Reputation: 2185

Too many values to unpack when creating a data frame from two lists

I have list c and p and both have 35300 elements. I try to create a pandas data frame but there´s an error message when I run the code. How Can I fix this?

import pandas as pd

e=pd.DataFrame.from_items(['Company',c],['ID',p])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-284-89427a7d8af3> in <module>()
      1 import pandas as pd
      2 
----> 3 e=pd.DataFrame.from_items(['Company',c],['ID',p])

C:\Users\toshiba\Anaconda\lib\site-packages\pandas\core\frame.pyc in from_items(cls, items, columns, orient)
   1195         frame : DataFrame
   1196         """
-> 1197         keys, values = zip(*items)
   1198 
   1199         if orient == 'columns':

ValueError: too many values to unpack

Upvotes: 1

Views: 989

Answers (1)

unutbu
unutbu

Reputation: 880489

Since c and p are lists, it sounds like you want to define a DataFrame with two columns, Company and ID:

e = pd.DataFrame({'Company':c, 'ID':p})

As behzad.nouri's suggests,

e = pd.DataFrame.from_items([('Company',c), ('ID',p)])

would also work, and unlike my first suggestion, would fix the order of the columns.

Upvotes: 2

Related Questions