Python Pandas: How to Convert GroupBy Results to Dictionary of Lists/DataFrames
After performing a groupby() operation in Pandas, you often get a DataFrameGroupBy object. A common next step is to convert the results of this grouping into a Python dictionary, where keys are the group names and values are lists (or even sub-DataFrames) of the corresponding group's data. This format can be convenient for further processing, JSON serialization, or custom data structuring.
This guide explains how to convert Pandas GroupBy results into a dictionary of lists or a dictionary of DataFrames.
Understanding GroupBy Objects
When you call df.groupby('ColumnA'), Pandas creates a DataFrameGroupBy object. This object itself doesn't immediately show the groups; rather, it holds information about how the DataFrame has been split. You can iterate over this object to get (group_name, group_dataframe) pairs, or apply aggregation functions (.sum(), .mean(), .size(), .apply(), etc.) to it.
Our goal is to take these (group_name, group_data) relationships and structure them into a Python dictionary.
Example DataFrame
import pandas as pd
data = {
'Department': ['Sales', 'HR', 'Engineering', 'Sales', 'HR', 'Engineering', 'Sales'],
'Employee': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace'],
'Salary': [70000, 60000, 95000, 72000, 65000, 105000, 68000],
'Project': ['P1', 'P2', 'P3', 'P1', 'P3', 'P4', 'P2']
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Output:
Original DataFrame:
Department Employee Salary Project
0 Sales Alice 70000 P1
1 HR Bob 60000 P2
2 Engineering Charlie 95000 P3
3 Sales David 72000 P1
4 HR Eve 65000 P3
5 Engineering Frank 105000 P4
6 Sales Grace 68000 P2
We'll group by 'Department' and create dictionaries from the 'Salary' or 'Employee' columns, or entire sub-DataFrames.