Why use groupby(...).transform("mean") instead of groupby(...).agg("mean") in pandas?
Correct answer
C. transform broadcasts group results back to every original row
14 views
C. transform broadcasts group results back to every original row
14 views
transform returns an object aligned to the original index, so each row receives its group's aggregate and the column can be assigned back with df["group_mean"] = df.groupby("k")["v"].transform("mean"). agg collapses each group to a single row instead, which is what you want for a summary table but not for filling gaps or computing within-group deviations. The transform trick is the standard vectorised way to normalise a value against its group without a separate merge.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is C? Ask here and get an explanation.