How to group data with pandas groupby: step by step
Grouping data with pandas groupby is one of the most useful tasks in Python for data analysis: it summarises thousands of rows into per-category results and answers questions like "what is the revenue per region?" or "how many sales per product?". Instead of filtering category by category by hand, groupby does that work in one go, with short and readable code. Below is a practical example, from the simplest case to a full summary, to help you master groupby and data aggregation.
Prerequisites
- Python 3 installed on your computer.
- The pandas library installed (
pip install pandas). - Basic knowledge of DataFrames: a table with rows and columns.
Step 1: Prepare the sample data
So you can see groupby working without depending on external files, we start by creating a small DataFrame of sales. Each row is a sale and has four columns: the region, the product, the units sold and the revenue.
import pandas as pd
sales = pd.DataFrame({
"region": ["North", "North", "South", "South", "Center", "North"],
"product": ["A", "B", "A", "B", "A", "A"],
"units": [10, 5, 8, 12, 7, 3],
"revenue": [100, 75, 80, 180, 70, 30],
})
Step 2: Group by one column and sum
The groupby pattern is called "split-apply-combine": pandas splits the rows by each value of the chosen column, applies a function to each group (here, the sum) and combines the results into a single structure. To get the revenue per region, we group by region, select the revenue column and apply sum():
revenue_by_region = sales.groupby("region")["revenue"].sum()
print(revenue_by_region)
The result is a Series whose index becomes the region: Center adds up to 70, North to 205 and South to 260. Notice that the column we grouped by is no longer an ordinary column and becomes the index of the result.
Step 3: Apply several aggregations with agg()
In practice we almost always want more than one metric. With the agg() method and the named aggregation style we give each result a name and specify, for each one, the column and the function to apply. This produces several summary columns at once, already with clear names:
summary = sales.groupby("region").agg(
total_revenue=("revenue", "sum"),
avg_units=("units", "mean"),
n_sales=("revenue", "count"),
)
print(summary)
Each row of the result is a region and each column is a metric: the total revenue, the average number of units (mean) and the number of sales (count). Other widely used functions are min, max, median, std and nunique (distinct values).
Step 4: Group by several columns
We can group by more than one column by passing a list to groupby. To get the revenue per region and per product, we group by both:
by_region_product = (
sales.groupby(["region", "product"])["revenue"].sum().reset_index()
)
print(by_region_product)
Now the result has one row per combination of region and product. When you group by several columns, pandas creates a composite index (MultiIndex); reset_index() turns that index into ordinary columns and returns a clean DataFrame, ready to use in a chart or export to a file.
Verify the result
A simple way to confirm the grouping is correct is to compare the sum of all groups with the sum of the original column. If the two values differ, something went wrong — for example, missing values (NaN) that were not counted:
print(revenue_by_region.sum()) # 535
print(sales["revenue"].sum()) # 535
The two values match (535), so groupby did not lose or duplicate any rows. To read the result faster, you can sort it with revenue_by_region.sort_values(ascending=False) and see the highest-revenue regions first.
Conclusion
With groupby you moved from a table of loose rows to clear answers per category — the foundation of almost every data analysis in pandas. From here, two natural next steps are to combine groupby with merge to join information from several tables, and to build a pivot table with pivot_table. What is the first question you will answer from your data with a groupby?