← Back to Pandas Course | Chapter 7: Grouping & Aggregation | Lesson 4 of 7

groupby with transform

transform returns a result the same size as the original, ideal for adding group statistics as new columns.

In this page:

  1. groupby with transform
Syntax
python
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

python
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
  1. Using agg and then trying to merge back by hand
  2. Returning a different length from the function
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.