What is the difference between agg(), transform(), and filter() when used with groupby()?

Quick Overview

Asks how pandas groupby agg(), transform() and filter() differ. Tests whether you can predict the shape and index of each result, pick the right method for group summaries, per-row group features and dropping whole groups, and avoid common predicate and alignment errors.

What is the difference between agg(), transform(), and filter() when used with groupby()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What is the difference between `agg()`, `transform()`, and `filter()` when used with `groupby()`? ```hint Compare the outputs For each method, compare the number of rows and the index of the result with the original DataFrame. ``` ### What a Strong Answer Covers - The shape and index of the result each method returns, compared with the input. - What kind of function each method expects from you, and what happens when the function returns the wrong kind of value. - A typical use case for each, such as group summaries, group-wise normalization or filling, and dropping small groups. - Practical pitfalls, such as assigning an aggregate back to the rows or writing a filter predicate that returns one value per row. ### Follow-up Questions - How would you add a column with each row's share of its group's total? - How would you keep only the rows whose score is above their own group's mean, and why can `filter()` not do that directly? - How does `apply()` differ from these three methods, and why is it usually slower? - Why can `transform("mean")` be much faster than `transform(lambda x: x.mean())`?

Overview: Asks how pandas groupby agg(), transform() and filter() differ. Tests whether you can predict the shape and index of each result, pick the right method for group summaries, per-row group features and dropping whole groups, and avoid common predicate and alignment errors.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What is the difference between agg(), transform(), and filter() when used with groupby()?

What a Strong Answer Covers Guidance

  • The shape and index of the result each method returns, compared with the input.
  • What kind of function each method expects from you, and what happens when the function returns the wrong kind of value.
  • A typical use case for each, such as group summaries, group-wise normalization or filling, and dropping small groups.
  • Practical pitfalls, such as assigning an aggregate back to the rows or writing a filter predicate that returns one value per row.

Follow-up Questions Guidance

  • How would you add a column with each row's share of its group's total?
  • How would you keep only the rows whose score is above their own group's mean, and why can filter() not do that directly?
  • How does apply() differ from these three methods, and why is it usually slower?
  • Why can transform("mean") be much faster than transform(lambda x: x.mean()) ?