Keep Each Student's Highest-Score Row in pandas

Quick Overview

Write a pandas function that keeps one row per student holding that student's highest score, sorted by student with a clean index. It tests sort_values with drop_duplicates, index handling, tie-breaking rules for equal scores, and a groupby idxmax alternative.

Keep Each Student's Highest-Score Row in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `keep_max_score_per_student(df: pd.DataFrame) -> pd.DataFrame`. The input has columns `['student', 'score']`, with an integer `score`. Keep only the single row with each student's maximum score. The function must: - Use `sort_values('score')` ascending, then `drop_duplicates('student', keep='last')`. - Sort the result by `student` ascending. - Reset the index to `0..n-1`, dropping the old index. - Return both columns `['student', 'score']` as a new DataFrame without mutating the input. You may assume each student's maximum score is unique (no ties). For example: ```python df = pd.DataFrame({ 'student': ['ana', 'bo', 'ana', 'bo'], 'score': [70, 90, 85, 60], }) keep_max_score_per_student(df) # student score # 0 ana 85 # 1 bo 90 ``` ```hint Row order after the drop After dropping duplicates, the surviving rows are still in score order and keep their old index labels. Check both before you return. ``` ### What a Strong Answer Covers - Why an ascending sort by score followed by `keep='last'` selects each student's maximum row. - Reordering by student and resetting the index to `0..n-1` after deduplication. - Returning a new frame with exactly `student` and `score`, leaving the input unchanged. - How ties on the maximum would be resolved and which tied row would win. - Time and space complexity, compared with a `groupby`-based alternative. ### Follow-up Questions - How would you handle ties on the max score deterministically? - Could you achieve the same result with `groupby` instead of `drop_duplicates`? - How would you keep each student's top two scores instead of only the maximum?

Overview: Write a pandas function that keeps one row per student holding that student's highest score, sorted by student with a clean index. It tests sort_values with drop_duplicates, index handling, tie-breaking rules for equal scores, and a groupby idxmax alternative.

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

Implement keep_max_score_per_student(df: pd.DataFrame) -> pd.DataFrame. The input has columns ['student', 'score'], with an integer score. Keep only the single row with each student's maximum score.

The function must:

  • Use sort_values('score') ascending, then drop_duplicates('student', keep='last') .
  • Sort the result by student ascending.
  • Reset the index to 0..n-1 , dropping the old index.
  • Return both columns ['student', 'score'] as a new DataFrame without mutating the input.

You may assume each student's maximum score is unique (no ties).

For example:

df = pd.DataFrame({
    'student': ['ana', 'bo', 'ana', 'bo'],
    'score':   [70,    90,   85,    60],
})

keep_max_score_per_student(df)
#   student  score
# 0     ana     85
# 1      bo     90

What a Strong Answer Covers Guidance

  • Why an ascending sort by score followed by keep='last' selects each student's maximum row.
  • Reordering by student and resetting the index to 0..n-1 after deduplication.
  • Returning a new frame with exactly student and score , leaving the input unchanged.
  • How ties on the maximum would be resolved and which tied row would win.
  • Time and space complexity, compared with a groupby -based alternative.

Follow-up Questions Guidance

  • How would you handle ties on the max score deterministically?
  • Could you achieve the same result with groupby instead of drop_duplicates ?
  • How would you keep each student's top two scores instead of only the maximum?