Search code examples
pythondictionarypandasnesteddataframe

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?


Solution

  • 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