transform
df.groupby("k")["v"].transform("sum") computes the sum per group and returns it on every original row, so the result aligns with df and can be assigned as a new column. Use it for shares of a group total, group-wise normalization or filling by group.
Syntax
grouped.transform(func) | Series.transform(func)| Part | What it is |
|---|---|
func | A function name string ("sum", "mean", "max") or a callable that returns a same-length Series or a scalar |
Examples
import pandas as pd
df = pd.DataFrame({"dept": ["Sales", "IT", "Sales", "IT"],
"salary": [50000, 62000, 58000, 70000]})
df["dept_total"] = df.groupby("dept")["salary"].transform("sum")
dfResult: dept salary dept_total 0 Sales 50000 108000 1 IT 62000 132000 2 Sales 58000 108000 3 IT 70000 132000
df["share"] = (df["salary"] / df["dept_total"]).round(3)
df["share"].tolist()Result: [0.463, 0.47, 0.537, 0.53]
df["vs_dept_mean"] = df["salary"] - df.groupby("dept")["salary"].transform("mean")
df["vs_dept_mean"].tolist()Result: [-4000.0, -4000.0, 4000.0, 4000.0]
Common mistakes
- Using agg and trying to assign to a column gives a length mismatch: agg returns one row per group; transform returns one per original row.
- A custom function returns a scalar for some groups and a Series for others: return consistently, either a scalar or a same-length Series.
- transform("size") or "count" to number rows: use groupby(...).cumcount() for a running counter.
Practice transform
These drills check your answer on live data, free, no account needed.
- Total by groupReturn the total `revenue` for each region as a Series indexed by region.
- Running totalReturn the running total of `signups`, day by day.
Questions
transform vs apply in groupby?
transform must return output the same length as each group (or a scalar to broadcast) and is faster. apply can return any shape, including a reduced frame per group.
How do I filter groups by a group statistic?
Build the mask with transform: df[df.groupby("k")["v"].transform("sum") > 100]. groupby(...).filter(lambda g: g.v.sum() > 100) does the same.
Related
- groupbyHow groupby works in pandas: group by one or more columns, apply sum, mean, count or size, get a flat table with as_index=False, and iterate over groups.
- aggHow to use agg in pandas to compute several statistics per group, different functions per column, named aggregations with flat column names, and lambdas.
- applyHow to use apply in pandas on a Series or DataFrame, with axis=1 for row-wise logic, lambdas, returning several columns, and why vectorized code is faster.
- fillnaHow to fill NaN values in pandas with fillna: a constant, a different value per column, the column mean or median, forward and backward fill, and by group.