groupby with transform
transform returns a result the same size as the original, ideal for adding group statistics as new columns.
In this page:
Syntax
df["new_column"] = df.groupby("group_column")["value_column"].transform("function_name")
groupby with transform
Unlike agg, which shrinks each group to one value, transform broadcasts a per-group result back to every row. That lets you add columns like each row's share of its group total or its deviation from the group mean. The output index matches the input.
Note:
df["pct"] = df["x"] / df.groupby("g")["x"].transform("sum") gives group shares.
Example: groupby with transform
import pandas as pd
df = pd.DataFrame({"dept": ["A", "A", "B", "B"], "salary": [100, 300, 200, 200]})
df["dept_mean"] = df.groupby("dept")["salary"].transform("mean")
df["share"] = df["salary"] / df.groupby("dept")["salary"].transform("sum")
print(df)
# Output:
# dept salary dept_mean share
# 0 A 100 200.0 0.25
# 1 A 300 200.0 0.75
# 2 B 200 200.0 0.50
# 3 B 200 200.0 0.50
Related Topics
Common Mistakes
- Using agg and then trying to merge back by hand
- Returning a different length from the function
- Confusing transform with apply
Chapter Summary
- transform keeps the original length
- It broadcasts group results to rows
- Perfect for shares and deviations
- agg reduces, transform does not
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: