DataFrame from items in nested dictionary

PHOTO EMBED

Fri Nov 26 2021 11:46:13 GMT+0000 (Coordinated Universal Time)

Saved by @adamyalei #python

user_dict = {12: {'Category 1': {'att_1': 1, 'att_2': 'whatever'},
                  'Category 2': {'att_1': 23, 'att_2': 'another'}},
             15: {'Category 1': {'att_1': 10, 'att_2': 'foo'},
                  'Category 2': {'att_1': 30, 'att_2': 'bar'}}}

pd.DataFrame.from_dict({(i,j): user_dict[i][j] 
                           for i in user_dict.keys() 
                           for j in user_dict[i].keys()},
                       orient='index')


               att_1     att_2
12 Category 1      1  whatever
   Category 2     23   another
15 Category 1     10       foo
   Category 2     30       bar
content_copyCOPY

https://stackoverflow.com/questions/13575090/construct-pandas-dataframe-from-items-in-nested-dictionary