Convert Pandas Dataframe To Nested Dictionary

Converting a Pandas DataFrame to a nested dictionary involves organizing the data in a hierarchical structure based on specific columns. In Python’s Pandas library, we can utilize the groupby function along with apply to create groupings based on chosen columns.

Let’s create a DataFrame that contains information about different products, including their names, prices, and stock quantities. The to_dict method with the parameter orient=records is commonly employed to convert each group into a dictionary of records. Let’s see how to convert dataframe to nested dictionary:

Using to_dict

Python3




import pandas as pd
 
# Your DataFrame
data = {'Name': ['Emily', 'David', 'Emily', 'David'],
        'Chain': ['Panera Bread', 'Starbucks', 'Subway', 'Chick-fil-A'],
        'Food': ['soup', 'coffee', 'sandwich', 'grilled chicken'],
        'Healthy': [True, False, True, True]}
 
df = pd.DataFrame(data)
 
# Convert DataFrame to nested dictionary
nested_dict = df.groupby('Name').apply(lambda x: x[['Chain', 'Food', 'Healthy']].to_dict(orient='records')).to_dict()
 
print(nested_dict)


Output:

{'David': [{'Chain': 'Starbucks', 'Food': 'coffee', 'Healthy': False}, 
{'Chain': 'Chick-fil-A', 'Food': 'grilled chicken', 'Healthy': True}],
'Emily': [{'Chain': 'Panera Bread', 'Food': 'soup', 'Healthy': True},
{'Chain': 'Subway', 'Food': 'sandwich', 'Healthy': True}]}

Let’s see another complex example, to gain in-depth understanding.

Python3




import pandas as pd
data = {'Person': ['Alice', 'Bob', 'Alice', 'Bob'],
                'Location': ['Home', 'Work', 'Home', 'Work'],
                'Activity': ['Cooking', 'Meeting', 'Reading', 'Working'],
                'Duration': [30, 60, 45, 120]}
 
df = pd.DataFrame(data)
nested_dict_another = df.groupby(['Person', 'Location']).apply(lambda x: x[['Activity', 'Duration']].to_dict(orient='records')).to_dict()
print(nested_dict_another)


Output:

{('Alice', 'Home'): [{'Activity': 'Cooking', 'Duration': 30}, {'Activity': 'Reading', 'Duration': 45}], 
('Bob', 'Work'): [{'Activity': 'Meeting', 'Duration': 60}, {'Activity': 'Working', 'Duration': 120}]}

How To Convert Pandas Dataframe To Nested Dictionary

In this article, we will learn how to convert Pandas DataFrame to Nested Dictionary.

Similar Reads

Convert Pandas Dataframe To Nested Dictionary

Converting a Pandas DataFrame to a nested dictionary involves organizing the data in a hierarchical structure based on specific columns. In Python’s Pandas library, we can utilize the groupby function along with apply to create groupings based on chosen columns....

Conclusion

...