Construct Pandas Dataframe From Nested Dictionaries Having List As Item
I have several dictionary data and I want to convert to Pandas DataFrame. However, due to unnecessary key '0' (for me), I've obtained undesirable format of DataFrame when I convert
Solution 1:
You should simply drop a level from your nested dict to make life easier. The code below drops the unnecessary part of your dicts and concatenates the dataframes from each of the dicts together.
all_dicts=[dict1,dict2,dict3]
df=pd.concat([pd.DataFrame({k:v[0] for k,v in d.items()}) for d in all_dicts])
df.index=pd.MultiIndex.from_product([['dict1','dict2','dict3'],['A','B']])
>>> df
1 2 3 4 5
dict1 A -0.022 0.269 0.118 0.057 -0.916
B -0.017 0.271 0.119 0.061 -0.924
dict2 A 0.384 0.485 0.465 0.456 -0.479
B 0.398 0.489 0.469 0.468 -0.482
dict3 A -0.323 -0.535 -0.336 -0.140 0.175
B -0.321 -0.534 -0.336 -0.142 0.177
Solution 2:
You can simply modify your input data and convert it to DataFrame
:
import itertools
lst = [dict1, dict2, dict3]
dict = {}
for k in dict1:
temp = [l[k].itervalues().next() for l in lst]
dict[k] = list(itertools.chain(*temp))
dict['row'] = ['A','B']*len(lst)
dict['dict'] = ['dict'+str(i+1) for i inrange(len(lst)) for n inrange(2)]
In [23]: pd.DataFrame(dict)
Out[23]:
12345dict row
0 -0.0220.2690.1180.057 -0.916 dict1 A
1 -0.0170.2710.1190.061 -0.924 dict1 B
20.3840.4850.4650.456 -0.479 dict2 A
30.3980.4890.4690.468 -0.482 dict2 B
4 -0.323 -0.535 -0.336 -0.1400.175 dict3 A
5 -0.321 -0.534 -0.336 -0.1420.177 dict3 B
Post a Comment for "Construct Pandas Dataframe From Nested Dictionaries Having List As Item"