简体   繁体   中英

Create pandas dataframe from nested dict with outer keys as df index and inner keys column headers

I have a nested dictionary like below

dictA = {X:{A: 0.2, B:0.3, C:0.4} ,Y:{A: 0.05, B:0.8, C:0.1},Z:{A: 0.15, B:0.6, C:0.25}}

I want to create a dataframe where the first key corresponds to the index and the keys of the nested dictionaries are the column headers. For example,

     A    B    C
  X  0.2  0.3  0.4 
  Y  0.05 0.8  0.1
  Z  0.15 0.6  0.25

I know I can pull out the keys, from the outer dict, into a list (using a list comprehension):

index_list = [key for key in dictA.iterkeys()]

and then the nested dictionaries into a single dictionary:

dict_list = [value for value in dictA.itervalues()]
final_dict = {k: v for dict in dict_list for k, v in dict.items()}

Finally I could create my df by:

df = pd.DataFrame(final_dict, index = index_list)

The problem is i need to map the correct values back to the correct index which is difficult when the ordinary of dictionary changes.

I imagine there is a completely different and more efficient way than what I have suggested above, help please?

Use from_dict and pass orient='index' it's designed to handle this form of dict:

In [350]:
pd.DataFrame.from_dict(dictA, orient='index')

Out[350]:
      A     C    B
X  0.20  0.40  0.3
Y  0.05  0.10  0.8
Z  0.15  0.25  0.6

You can simply convert your dictA to a DataFrame and then take transpose, to make columns into index and index into columns. Example -

df = pd.DataFrame(dictA).T

Demo -

In [182]: dictA = {'X':{'A': 0.2, 'B':0.3, 'C':0.4} ,'Y':{'A': 0.05, 'B':0.8, 'C':0.1},'Z':{'A': 0.15, 'B':0.6, 'C':0.25}}

In [183]: df = pd.DataFrame(dictA).T

In [184]: df
Out[184]:
      A    B     C
X  0.20  0.3  0.40
Y  0.05  0.8  0.10
Z  0.15  0.6  0.25

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM