Add a descending dense rank column to a pandas DataFrame

Quick Overview

Write a pandas function that returns a copy of a DataFrame with a dense rank of its score column, highest score first and tied scores sharing a rank with no gaps. It tests Series.rank options, tie handling, avoiding mutation of the input, and the dtype pandas returns for ranks.

Add a descending dense rank column to a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def dense_rank_scores(df: pd.DataFrame) -> pd.DataFrame` that returns a copy of `df` with a new column `'rnk'` holding the dense rank of the integer column `'score'` in descending order, computed with `df['score'].rank(method='dense', ascending=False)`. Requirements: - Do not mutate the input: operate on a copy of `df`. - The highest score gets rank 1. - Tied scores share a rank, and the next rank is the following integer (no gaps). - Keep the original `'score'` column. `'rnk'` is float dtype. For example: ```python df = pd.DataFrame({'score': [90, 90, 80, 70]}) dense_rank_scores(df) # score rnk # 0 90 1.0 # 1 90 1.0 # 2 80 2.0 # 3 70 3.0 ``` ```hint Where the column goes Adding a column to `df` itself changes the caller's frame. Decide which object receives `'rnk'` before you call `rank`. ``` ### What a Strong Answer Covers - Working on a copy so the caller's DataFrame is unchanged - Ranking in descending order so the highest score gets rank 1 - How dense ranking treats ties and why it leaves no gaps - Why the rank column comes back as float, and what a missing score would produce ### Follow-up Questions - How does `method='dense'` differ from `'min'` and `'first'`? - How would you return `'rnk'` as an integer dtype? - How would you write the same ranking in SQL?

Overview: Write a pandas function that returns a copy of a DataFrame with a dense rank of its score column, highest score first and tied scores sharing a rank with no gaps. It tests Series.rank options, tie handling, avoiding mutation of the input, and the dtype pandas returns for ranks.

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

Implement def dense_rank_scores(df: pd.DataFrame) -> pd.DataFrame that returns a copy of df with a new column 'rnk' holding the dense rank of the integer column 'score' in descending order, computed with df['score'].rank(method='dense', ascending=False).

Requirements:

  • Do not mutate the input: operate on a copy of df .
  • The highest score gets rank 1.
  • Tied scores share a rank, and the next rank is the following integer (no gaps).
  • Keep the original 'score' column. 'rnk' is float dtype.

For example:

df = pd.DataFrame({'score': [90, 90, 80, 70]})
dense_rank_scores(df)
#    score  rnk
# 0     90  1.0
# 1     90  1.0
# 2     80  2.0
# 3     70  3.0

What a Strong Answer Covers Guidance

  • Working on a copy so the caller's DataFrame is unchanged
  • Ranking in descending order so the highest score gets rank 1
  • How dense ranking treats ties and why it leaves no gaps
  • Why the rank column comes back as float, and what a missing score would produce

Follow-up Questions Guidance

  • How does method='dense' differ from 'min' and 'first' ?
  • How would you return 'rnk' as an integer dtype?
  • How would you write the same ranking in SQL?