4

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?

maracuja
  • 300
  • 6
  • 22

2 Answers2

4

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
EdChum
  • 339,461
  • 188
  • 752
  • 538
2

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
Anand S Kumar
  • 82,977
  • 18
  • 174
  • 164