Skip to content Skip to sidebar Skip to footer

Creating A Panda Dataframe From Dictionary With Multiple Keys And Value (list) Of Different Lengths

I have a dictionary of with multiple keys with values(list) that do not have the same length. I would like to read them into a pandas DataFrame.I would like the keys to be the colu

Solution 1:

Use :

import pandas as pd 
dataframe1 = pd.DataFrame(dict([(k,pd.Series(v)) for k,v in my_dict.iteritems()]))  

where my_dict is your current dictionary.

Solution 2:

Not exactly sure what you want and I assume that you're getting the ValueError: arrays must all be same length error. A crude work around is simply backfill each list so that all of each list are the same length, then simply pass it to the DataFrame constructor. See example below:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: mydata = {'dict_{:02d}'.format(i): range(1, i+1) for i inrange(1, 5)}

In [4]: mydata
Out[4]:
{'dict_01': [1],
 'dict_02': [1, 2],
 'dict_03': [1, 2, 3],
 'dict_04': [1, 2, 3, 4]}

In [5]: max_len = max([len(x) for x in mydata.values()])

In [6]: max_len
Out[6]: 4

In [7]: df = pd.DataFrame({key: vals + [np.nan]*(max_len - len(vals)) for key, vals in mydata.iteritems()})

In [8]: df
Out[8]:
   dict_01  dict_02  dict_03  dict_04
011111      NaN        2222      NaN      NaN        333      NaN      NaN      NaN        4

Post a Comment for "Creating A Panda Dataframe From Dictionary With Multiple Keys And Value (list) Of Different Lengths"