I have a nested dictionary with 3 levels,
example_d = {'attribute_001': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_002': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_003': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_004': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}},
'attribute_005': {'colour': {'blue': 5, 'green': 5, 'red': 5},
'country': {'France': 3, 'Germany': 3, 'India': 3, 'UK': 3, 'USA': 3}}}
and I want to move this into a pandas dataframe such that the row-index is sourced at the first level of my dictionary, and use the remaining levels as hierarchical column indices.
I can get close by using the following, adapting an answer from here:
pd.concat({key:pd.DataFrame.from_dict(example_d[key],orient='columns')
for key in example_d.keys()}).unstack(1)
which gives me:
But I need the lowest level of the multi-level column indices to respect their parents.
i.e. under the colour
heading, I want only colour-columns to appear, and under the country
heading, I want to see only country-columns.