How does df.groupby('k')['x'].transform('mean') differ from .apply('mean')?
Correct answer
C. transform returns a Series aligned to the original index with the same length, while apply returns one value per group
13 views
C. transform returns a Series aligned to the original index with the same length, while apply returns one value per group
13 views
transform versus apply: transform broadcasts a group-level result back to every row of that group, so df['x'] - df.groupby('k')['x'].transform('mean') centres values within groups. apply returns an aggregated object with one row per group unless the function returns a like-indexed object. Filtering whole groups by size uses filter, a third distinct method.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is C? Ask here and get an explanation.