1

I've got a dictionaries with two nested dictionaries inside. Here is a minimal example :

df = {'line1': {'a': {'a': 1, 'b': 2}, 'b': {'a': 3, 'b': 4}},
      'line2': {'a': {'a': 5, 'b': 6}, 'b': {'a': 7, 'b': 8}},
      'line3': {'a': {'a': 9, 'b': 10}, 'b': {'a': 11, 'b': 12}},
      'line4': {'a': {'a': 13, 'b': 14}, 'b': {'a': 15, 'b': 16}}}

I have hundreds of lines of codes in my program, and this structure seemed to be the easiest to store every data I need. I want to create a pandas dataFrame like this :

        aa  ab  ba  bb
line1   1   2   3   4
line2   5   6   7   8
line3   9   10  11  12
line4   13  14  15  16

But it seems that the pd.DataFrame(dict) command do not handles correctly more than 2 nested dictionaries, I got this :

pd.DataFrame(df).T

                          a                     b
line1    {u'a': 1, u'b': 2}    {u'a': 3, u'b': 4}
line2    {u'a': 5, u'b': 6}    {u'a': 7, u'b': 8}
line3   {u'a': 9, u'b': 10}  {u'a': 11, u'b': 12}
line4  {u'a': 13, u'b': 14}  {u'a': 15, u'b': 16}

Is there an easy way to solve this ? Or do I have to write additional code to transform my dictionary, or worse, to design another way to store my data (that would make me re-write several parts of the program) ?

Thanks in advance.

Micawber
  • 707
  • 1
  • 5
  • 19
  • See https://stackoverflow.com/questions/24988131/nested-dictionary-to-multiindex-dataframe-where-dictionary-keys-are-column-label – John Zwinck Feb 21 '18 at 13:29
  • I had a quick search among already existing questions but didn't find that one, sorry. It works now :) – Micawber Feb 21 '18 at 13:39

1 Answers1

3

Use concat with dict comprehension, then reshape data by unstack and convert MultiIndex by map:

df = pd.concat({k: pd.DataFrame(v) for k, v in df.items()}).unstack()
df.columns = df.columns.map(''.join)
print (df)
       aa  ab  ba  bb
line1   1   2   3   4
line2   5   6   7   8
line3   9  10  11  12
line4  13  14  15  16
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Perfect ! Thank you ! I had a quick search among already existing questions but didn't find that one, sorry. – Micawber Feb 21 '18 at 13:36