Grade Scores With pd.cut

Quick Overview

Write a pandas function that maps integer scores from 0 to 100 to letter grades with pd.cut and appends them as an ordered categorical column without changing the input. It tests interval boundaries, how cut handles edge values and how categorical ordering supports comparisons.

Grade Scores With pd.cut

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `grade_scores(df)`. The input has an integer column `'score'` in 0..100. Return a new DataFrame equal to the input plus an appended `'grade'` column from `pd.cut(df['score'], bins=[0,60,70,80,90,101], labels=['F','D','C','B','A'], right=False, include_lowest=True)`. The function must meet these requirements: - Do not mutate the input. Original columns and index stay unchanged. - Intervals are left-inclusive: `[0,60),[60,70),[70,80),[80,90),[90,101)`. - `'grade'` is an ordered Categorical with `F < D < C < B < A`. Example: ```text score grade 60 D # 60 falls in [60,70) 89 B # 89 falls in [80,90) 90 A # 90 falls in [90,101) 100 A ``` ```hint Trace the boundaries Work out which side of each interval `pd.cut` closes by default, then trace the scores 0, 60, 90 and 100 through the bins with and without `right=False`. ``` ### Constraints & Assumptions - `'score'` holds integers from 0 to 100. - Out-of-range scores are out of scope. ### What a Strong Answer Covers - Copying the frame (or using `assign`) so the input's columns and index are unchanged. - Which bin each boundary score lands in, including why the last edge is 101 and not 100. - What `right=False` and `include_lowest=True` each change, and which one actually affects these bins. - How the resulting Categorical gets its order and what that allows (comparisons, sorting). ### Follow-up Questions - Why does `right=False` matter here versus the default? - How would you keep the ordering so comparisons like `df['grade'] >= 'C'` work? - How would you count students per grade so that a grade nobody received still shows up with a count of 0?

Overview: Write a pandas function that maps integer scores from 0 to 100 to letter grades with pd.cut and appends them as an ordered categorical column without changing the input. It tests interval boundaries, how cut handles edge values and how categorical ordering supports comparisons.

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

Implement grade_scores(df). The input has an integer column 'score' in 0..100. Return a new DataFrame equal to the input plus an appended 'grade' column from pd.cut(df['score'], bins=[0,60,70,80,90,101], labels=['F','D','C','B','A'], right=False, include_lowest=True).

The function must meet these requirements:

  • Do not mutate the input. Original columns and index stay unchanged.
  • Intervals are left-inclusive: [0,60),[60,70),[70,80),[80,90),[90,101) .
  • 'grade' is an ordered Categorical with F < D < C < B < A .

Example:

score  grade
  60     D    # 60 falls in [60,70)
  89     B    # 89 falls in [80,90)
  90     A    # 90 falls in [90,101)
 100     A

Constraints & Assumptions

  • 'score' holds integers from 0 to 100.
  • Out-of-range scores are out of scope.

What a Strong Answer Covers Guidance

  • Copying the frame (or using assign ) so the input's columns and index are unchanged.
  • Which bin each boundary score lands in, including why the last edge is 101 and not 100.
  • What right=False and include_lowest=True each change, and which one actually affects these bins.
  • How the resulting Categorical gets its order and what that allows (comparisons, sorting).

Follow-up Questions Guidance

  • Why does right=False matter here versus the default?
  • How would you keep the ordering so comparisons like df['grade'] >= 'C' work?
  • How would you count students per grade so that a grade nobody received still shows up with a count of 0?