pandas groupby summarizes data by category through the split-apply-combine pattern. Choose one or more key columns, apply a sum, mean, count, or another operation, and receive a consolidated table. Typical questions include revenue by region, average order value by channel, and orders by month.
If the input still needs preparation, start with the pandas data analysis guide. Reliable grouping depends on correct dtypes, deliberate handling of missing values, and clear column names.
Your first groupby with sum and mean
import pandas as pd
sales = pd.DataFrame({
"region": ["South", "South", "North", "North"],
"channel": ["web", "store", "web", "store"],
"revenue": [1200, 800, 950, 1100],
"orders": [12, 5, 10, 7],
})
summary = (
sales.groupby("region", as_index=False)
.agg(total_revenue=("revenue", "sum"),
average_revenue=("revenue", "mean"),
total_orders=("orders", "sum"))
)
Named aggregation states which input column and function produce each output. as_index=False keeps region as a column. Without it, the key becomes the index, which is valid but may require reset_index() before an export or merge.
Group by multiple columns
Pass a list when each combination defines a group:
by_channel = (
sales.groupby(["region", "channel"], as_index=False)
.agg(revenue=("revenue", "sum"), orders=("orders", "sum"))
.sort_values("revenue", ascending=False)
)
Null grouping keys are excluded by default. Use dropna=False when an “unknown region†group must remain visible for auditing. Consider sort=False when input order matters and sorting keys adds unnecessary work.
agg, transform, and filter answer different questions
agg reduces each group. transform returns one value for every original row, making it suitable for shares and comparisons with a group average:
sales["region_average"] = (
sales.groupby("region")["revenue"].transform("mean")
)
sales["share"] = (
sales["revenue"]
/ sales.groupby("region")["revenue"].transform("sum")
)
filter keeps or removes complete groups:
relevant_regions = sales.groupby("region").filter(
lambda group: group["orders"].sum() >= 15
)
Prefer vectorized aggregations such as "sum", "mean", and "size" over apply with a Python callback. They express intent more clearly and usually run faster. Keep apply for logic that cannot be represented by a dedicated GroupBy operation.
Group time series
Convert dates before using pd.Grouper:
sales["date"] = pd.to_datetime(sales["date"], errors="raise")
monthly = (
sales.groupby(pd.Grouper(key="date", freq="MS"))
.agg(revenue=("revenue", "sum"))
.reset_index()
)
MS labels periods at each month start. Check time zones and granularity before comparing reports.
Common errors and validation
Do not use count when you mean “number of rows with nulls included,†because it skips missing values in that column. Use size for row counts. Confirm that numeric data did not arrive as text. Reconcile totals before and after grouping, and sort explicitly whenever presentation order matters.
The official pandas GroupBy guide, accessed July 28, 2026, covers aggregation, transformation, and filtering and recommends purpose-built operations before custom Python functions.