transform for Group-Wise Features
Aligning group stats back to rows.
agg Shrinks, transform Keeps
Where agg returns one row per group, transform returns a value for every original row, same length as the input.
Why That Matters
Because transform keeps the original shape, you can drop its result straight back into your DataFrame as a new column.
df["city_avg"] = df.groupby("city")["sales"].transform("mean")All lessons in this course
- Split-Apply-Combine Explained
- Multiple Aggregations With agg
- Group by Several Keys
- transform for Group-Wise Features