How can you perform multiple aggregations simultaneously on one or more columns using Pandas agg()?

Quick Overview

Asks how to compute several aggregations at once on one or more DataFrame columns with the Pandas agg() method. Tests the list, dict and named-aggregation forms, output column naming and MultiIndex headers, custom functions, and details such as count versus size.

How can you perform multiple aggregations simultaneously on one or more columns using Pandas agg()?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How can you perform multiple aggregations simultaneously on one or more columns using `agg()`? For example, with a DataFrame of `team`, `player` and `score`, you might want the mean, total and maximum score per team plus the number of players, all in one result. ```hint Watch the column names `agg()` accepts several argument shapes. For each one, think about what the output column headers look like. ``` ### What a Strong Answer Covers - The list, dict and named-aggregation forms, and the output shape each one produces - How output columns are named, including MultiIndex headers and how to flatten them - Using custom functions and giving them clear names - Behavior details such as `count` versus `size` with missing values - Whether the group keys end up in the index or as columns ### Follow-up Questions - How would you compute a weighted average per group, which needs two columns at once? - When would you use `transform()` instead of `agg()`? - Why can lambda aggregations be much slower than string names like `"mean"` on large data? - How would you name an output column that is not a valid Python identifier, such as `"avg score"`?

Overview: Asks how to compute several aggregations at once on one or more DataFrame columns with the Pandas agg() method. Tests the list, dict and named-aggregation forms, output column naming and MultiIndex headers, custom functions, and details such as count versus size.

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

How can you perform multiple aggregations simultaneously on one or more columns using agg()?

For example, with a DataFrame of team, player and score, you might want the mean, total and maximum score per team plus the number of players, all in one result.

What a Strong Answer Covers Guidance

  • The list, dict and named-aggregation forms, and the output shape each one produces
  • How output columns are named, including MultiIndex headers and how to flatten them
  • Using custom functions and giving them clear names
  • Behavior details such as count versus size with missing values
  • Whether the group keys end up in the index or as columns

Follow-up Questions Guidance

  • How would you compute a weighted average per group, which needs two columns at once?
  • When would you use transform() instead of agg() ?
  • Why can lambda aggregations be much slower than string names like "mean" on large data?
  • How would you name an output column that is not a valid Python identifier, such as "avg score" ?