What is the difference between agg and transform in a pandas groupby?
agg returns one row per group, shrinking the data. transform returns a result with the same length as the original, repeating each group value on every row, so you can add it as a column and do row-level calculations like shares.
Explain it simply
After grouping orders by city, agg("sum") gives you a tiny table: one total per city. transform("sum") gives you the city total written next to every order, so it lines up with the original rows. That alignment is what you need for things like "what share of its city's sales is this order?"
Example
import pandas as pd
df = pd.DataFrame({
"city": ["Pune", "Pune", "Delhi", "Delhi", "Delhi"],
"amount": [100, 300, 200, 400, 600],
})
print(df.groupby("city")["amount"].transform("mean").tolist())
# [200.0, 200.0, 400.0, 400.0, 400.0]
df["share"] = df["amount"] / df.groupby("city")["amount"].transform("sum")
print(df["share"].round(2).tolist())
# [0.25, 0.75, 0.17, 0.33, 0.5]Pune's total is 400, so the first order is 100 / 400 = 0.25. Delhi's total is 1200, so 200 / 1200 = 0.17.
A second use is filling missing values with the group average: df["x"] = df["x"].fillna(df.groupby("g")["x"].transform("mean")).
What to say in the interview
"agg collapses each group to one row; transform keeps the original shape and broadcasts the group result back. I use transform for group-wise normalisation, shares of total and group-based imputation, without needing a merge afterwards."
Common mistakes
- Using agg and then merging the result back, which is slower and more error-prone than transform.