Reputation: 2747
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 column names and the values to be my rows. Assuming that I have a dictionary with multiple keys, I tried:
dict
df=pd.from_dict(dict,orient="columns")
But it does not still work. What alternative can I have?
Upvotes: 0
Views: 4783
Reputation: 4077
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.
Upvotes: 2
Reputation: 1946
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 in range(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
0 1 1 1 1
1 NaN 2 2 2
2 NaN NaN 3 3
3 NaN NaN NaN 4
Upvotes: 1