Home >Backend Development >Python Tutorial >How to Create a Pandas DataFrame from a Nested Dictionary with Hierarchical Indexes?
In this scenario, you wish to create a pandas DataFrame from a nested dictionary where the hierarchy consists of:
The desired DataFrame should have User IDs as the index and categories and attributes as columns.
One efficient approach utilizes pandas' MultiIndex, which enables the creation of a multi-level index structure. To employ this method:
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'}}} df = 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') print(df) att_1 att_2 12 Category 1 1 whatever Category 2 23 another 15 Category 1 10 foo Category 2 30 bar
Alternatively, you can build the DataFrame incrementally through concatenation:
user_ids = [] frames = [] for user_id, d in user_dict.iteritems(): user_ids.append(user_id) frames.append(pd.DataFrame.from_dict(d, orient='index')) df = pd.concat(frames, keys=user_ids) print(df) att_1 att_2 12 Category 1 1 whatever Category 2 23 another 15 Category 1 10 foo Category 2 30 bar
The above is the detailed content of How to Create a Pandas DataFrame from a Nested Dictionary with Hierarchical Indexes?. For more information, please follow other related articles on the PHP Chinese website!