Mean Score per Student with pandas groupby

Quick Overview

Write a pandas function that averages each student's scores and returns a flat two-column DataFrame sorted by student name. It tests groupby aggregation, keeping the group key as a column with as_index=False, and how pandas orders and types the result.

Mean Score per Student with pandas groupby

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `mean_score_per_student(df)` that takes a pandas DataFrame with columns `student` and `score` and returns each student's average score as a DataFrame with columns `student` and `score`. Requirements: - `student` is a regular column, not the index (`as_index=False`). - Rows are ordered by `student` ascending. Example: ```python df = pd.DataFrame({ 'student': ['Ann', 'Bob', 'Ann', 'Bob'], 'score': [90, 70, 80, 60], }) mean_score_per_student(df) # student score # 0 Ann 85.0 # 1 Bob 65.0 ``` ```hint Where the key ends up Check what `groupby(...).mean()` returns by default and where the group key lands. Select the column you average before aggregating, and check what `groupby` already does with the order of its keys before you add a sort. ``` ### What a Strong Answer Covers - A grouped mean that averages only the `score` column within each student. - Keeping `student` as a regular column so the result is a two-column DataFrame. - How the ascending order by `student` is guaranteed, either by the `groupby` default or an explicit sort. - The output dtype (the mean is float even for integer scores) and what happens to missing scores or missing student names. ### Follow-up Questions - How would you also return how many scores each student has, in the same DataFrame? - How would you attach each student's mean to every original row without collapsing the frame? - How would you sort by mean score descending and break ties by student name? - What changes if some scores or some student names are NaN?

Overview: Write a pandas function that averages each student's scores and returns a flat two-column DataFrame sorted by student name. It tests groupby aggregation, keeping the group key as a column with as_index=False, and how pandas orders and types the result.

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

Write mean_score_per_student(df) that takes a pandas DataFrame with columns student and score and returns each student's average score as a DataFrame with columns student and score.

Requirements:

  • student is a regular column, not the index ( as_index=False ).
  • Rows are ordered by student ascending.

Example:

df = pd.DataFrame({
    'student': ['Ann', 'Bob', 'Ann', 'Bob'],
    'score':   [90, 70, 80, 60],
})
mean_score_per_student(df)
#   student  score
# 0     Ann   85.0
# 1     Bob   65.0

What a Strong Answer Covers Guidance

  • A grouped mean that averages only the score column within each student.
  • Keeping student as a regular column so the result is a two-column DataFrame.
  • How the ascending order by student is guaranteed, either by the groupby default or an explicit sort.
  • The output dtype (the mean is float even for integer scores) and what happens to missing scores or missing student names.

Follow-up Questions Guidance

  • How would you also return how many scores each student has, in the same DataFrame?
  • How would you attach each student's mean to every original row without collapsing the frame?
  • How would you sort by mean score descending and break ties by student name?
  • What changes if some scores or some student names are NaN?