Compute Min, Max and Mean per Group with Pandas Named Aggregation

Quick Overview

Write a pandas function that returns the min, max and mean of points for each team, indexed by team, with flat column names from named aggregation. It tests groupby aggregation, column naming, and how the list form of agg and missing values change the result.

Compute Min, Max and Mean per Group with Pandas Named Aggregation

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `multi_stat_per_group(df)`. Given a DataFrame with the columns `team` and `points`, return a DataFrame with the min, max and mean of `points` per team, using named aggregation so the column names come out flat. The result must meet these requirements: - It is indexed by `team`, sorted ascending. - It has exactly three columns, `min`, `max` and `mean`, in that order. Example: ```python df = pd.DataFrame({ 'team': ['A', 'A', 'B', 'B', 'B'], 'points': [10, 20, 5, 15, 25], }) multi_stat_per_group(df) # min max mean # team # A 10 20 15.0 # B 5 25 15.0 ``` ```hint Shape of the result Check what the groupby key becomes in the output and whether pandas already sorts it. Then look at the form of `agg` that lets you name each output column yourself. ``` ### What a Strong Answer Covers - Grouping by `team` so the team becomes the sorted index of the result. - Producing each statistic as its own flat, named column in the required order. - Why the list form of `agg` on a grouped DataFrame gives different column labels. - The dtypes of the output columns and how missing values affect them. ### Follow-up Questions - How would you flatten the column names if you used `.agg(['min', 'max', 'mean'])` instead? - How would you add a custom percentile aggregation? - How does the result change if some `points` or `team` values are missing?

Overview: Write a pandas function that returns the min, max and mean of points for each team, indexed by team, with flat column names from named aggregation. It tests groupby aggregation, column naming, and how the list form of agg and missing values change the result.

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

Implement multi_stat_per_group(df). Given a DataFrame with the columns team and points, return a DataFrame with the min, max and mean of points per team, using named aggregation so the column names come out flat.

The result must meet these requirements:

  • It is indexed by team , sorted ascending.
  • It has exactly three columns, min , max and mean , in that order.

Example:

df = pd.DataFrame({
    'team':   ['A', 'A', 'B', 'B', 'B'],
    'points': [10, 20, 5, 15, 25],
})

multi_stat_per_group(df)
#       min  max  mean
# team
# A      10   20  15.0
# B       5   25  15.0

What a Strong Answer Covers Guidance

  • Grouping by team so the team becomes the sorted index of the result.
  • Producing each statistic as its own flat, named column in the required order.
  • Why the list form of agg on a grouped DataFrame gives different column labels.
  • The dtypes of the output columns and how missing values affect them.

Follow-up Questions Guidance

  • How would you flatten the column names if you used .agg(['min', 'max', 'mean']) instead?
  • How would you add a custom percentile aggregation?
  • How does the result change if some points or team values are missing?