How do named aggregations work in pandas agg()?

Quick Overview

Asks how named aggregation works in a pandas groupby agg() call and why you would use it over a dict of functions. Tests working knowledge of grouped aggregation syntax, the shape of the result, and common pitfalls with output names and counts.

How do named aggregations work in pandas agg()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do named aggregations work in `agg()`? Explain how you write one after a pandas `groupby` and what problem it solves compared with passing a dict or list of functions. ```hint Look at the labels Run `agg({'sales': ['sum', 'mean']})` on a grouped frame and look at the column labels it returns. Ask what extra work you would need before that result is easy to merge or report. ``` ### What a Strong Answer Covers - What the keyword name and its value each control in the call - The accepted forms of the value and how they relate to each other - How the result's column labels compare with dict-of-lists aggregation - How the syntax changes when aggregating a single selected column - At least one practical pitfall, such as output names that are not valid Python identifiers ### Follow-up Questions - How would you name an output column that contains a space? - How do you apply a custom function, such as the 90th percentile, under a readable name? - What is the difference between `'count'` and `'size'` here, and when do they give different numbers? - How do you get the group key back as a regular column instead of the index?

Overview: Asks how named aggregation works in a pandas groupby agg() call and why you would use it over a dict of functions. Tests working knowledge of grouped aggregation syntax, the shape of the result, and common pitfalls with output names and counts.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How do named aggregations work in agg()? Explain how you write one after a pandas groupby and what problem it solves compared with passing a dict or list of functions.

What a Strong Answer Covers Guidance

  • What the keyword name and its value each control in the call
  • The accepted forms of the value and how they relate to each other
  • How the result's column labels compare with dict-of-lists aggregation
  • How the syntax changes when aggregating a single selected column
  • At least one practical pitfall, such as output names that are not valid Python identifiers

Follow-up Questions Guidance

  • How would you name an output column that contains a space?
  • How do you apply a custom function, such as the 90th percentile, under a readable name?
  • What is the difference between 'count' and 'size' here, and when do they give different numbers?
  • How do you get the group key back as a regular column instead of the index?