Home > Software design >  Convert nested dictionary to flat pandas dataframe
Convert nested dictionary to flat pandas dataframe

Time:05-14

How can I convert a nested dictionary into a flat pandas df? Something such as this:

my_dict = {'Level1': {'Level21': {'Level31': 'some_value_31'},
  'Level22': {'Level31': 'some_value_32'},
  'Level23': {'Level31: 'some_value_31', 'Level32': 'some_value41'},
  'Level24': {'Level31': 'some_value32', 'Level32': 'some_value42'}}}

To something like this:

Level1              Level31          Level32
Level21             Some_value31     NaN
Level22             Some_value32     NaN
Level23             Some_value31     Some_value41
Level24             Some_value32     Some_value42

I tried something like this:

df = pd.concat({k: pd.DataFrame(v).T for k, v in my_dict.items()})

But what happens is, Level1 actually moves down at the row level as opposed to becoming a column for level2 values:

                                Level31          Level32
Level1    Level21               Some_value31     NaN
          Level22               Some_value32     NaN
          Level23               Some_value31     Some_value41
          Level24               Some_value32     Some_value42

Is there some simple method to move this up, or pop this level1 value into the column for the level2 fields?

CodePudding user response:

Try this,

df = pd.DataFrame(my_dict["Level1"]).T.reset_index().rename(columns = {"index":"Level1"})

Output -

Level1 Level31 Level32
0 Level21 some_value_31 nan
1 Level22 some_value_32 nan
2 Level23 some_value_31 some_value41
3 Level24 some_value32 some_value42
  • Related