4

I was trying to create panda dataframe from list as below:

my_value = ['Aberarder Creek', 'Town of Plympton-Wyoming', 'Aylmer', '17-4091-
47723', '43.062, -82.109', 'Northern Pike Rock Bass Smallmouth Bass White 
Sucker']
df = pd.DataFrame(columns=['Lake Name','Municipality','MNRF 
District','Coordinates','Waterbody ID','Fish Species'],index=np.arange(0))
my_df = pd.DataFrame(my_value,columns=['Lake Name','Municipality','MNRF 
District','Coordinates','Waterbody ID','Fish Species'])
my_df.append(df,ignore_index=True)
df

Keep geeting error message like:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in 
create_block_manager_from_blocks(blocks, axes)
   4293                 blocks = [make_block(values=blocks[0],
-> 4294                                      placement=slice(0, 
len(axes[0])))]
   4295 

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in 
make_block(values, placement, klass, ndim, dtype, fastpath)
   2718 
-> 2719     return klass(values, ndim=ndim, fastpath=fastpath, 
placement=placement)
   2720 

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in 
__init__(self, values, ndim, fastpath, placement, **kwargs)
   1843         super(ObjectBlock, self).__init__(values, ndim=ndim, 
fastpath=fastpath,
-> 1844                                           placement=placement, **kwargs)
   1845 

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in __init__(self, values, placement, ndim, fastpath)
    114                              'implies %d' % (len(self.values),
--> 115                                              len(self.mgr_locs)))
    116 

ValueError: Wrong number of items passed 1, placement implies 6

I wonder how come the elements in my_value doesn't match the number of columns. So confused, I might have some misunderstanding of the panda basics, thank you!

0

1 Answer 1

4

You need [] for nested list for one row with multiple columns DataFrame:

my_df = pd.DataFrame([my_value],columns=['Lake Name','Municipality','MNRF District','Coordinates','Waterbody ID','Fish Species'])
print (my_df)
         Lake Name              Municipality MNRF District    Coordinates  \
0  Aberarder Creek  Town of Plympton-Wyoming        Aylmer  17-4091-47723   

      Waterbody ID                                       Fish Species  
0  43.062, -82.109  Northern Pike Rock Bass Smallmouth Bass White ...  
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.