Compute the percentile rank of every value in a pandas Series

Quick Overview

Write a pandas function that returns the ascending percentile rank of each value in a numeric Series, with tied values sharing an average rank and the original index kept. It tests how ranks are normalized by the count and how tied and missing values change the result.

Compute the percentile rank of every value in a pandas Series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `percentile_rank(s: pd.Series) -> pd.Series` that returns the ascending percentile rank of every value in a numeric Series. A value's percentile rank is its rank (1 for the smallest value) divided by the number of values. Requirements: - Return a float Series with values in `(0, 1]`. When the largest value is unique, it maps to `1.0`. - Preserve the original index of `s`. - Ties share the average of their ranks (`method='average'`). Example: ```python s = pd.Series([10, 20, 20, 40]) percentile_rank(s) # 0 0.250 # 1 0.625 # tie: avg of ranks 2 and 3 -> 2.5/4 # 2 0.625 # 3 1.000 # dtype: float64 ``` The two 20s would take ranks 2 and 3, so each gets the average 2.5, and 2.5 / 4 = 0.625. ```hint Check what pandas already offers pandas can rank a Series directly. Look at which of its options normalize the ranks and how it breaks ties. ``` ### What a Strong Answer Covers - Ranking in ascending order and dividing by the count of non-missing values - Average tie handling, checked against the tied values in the example - What the largest value maps to when it is tied - Keeping the original index and returning floats - How missing values are treated ### Follow-up Questions - How would you rank in descending order instead? - What happens to `NaN` values, and how would you control that? - If the largest value appears twice, what does it map to, and which tie method would make it `1.0`? - How would you compute the percentile rank within each group of another column?

Overview: Write a pandas function that returns the ascending percentile rank of each value in a numeric Series, with tied values sharing an average rank and the original index kept. It tests how ranks are normalized by the count and how tied and missing values change the result.

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

Implement percentile_rank(s: pd.Series) -> pd.Series that returns the ascending percentile rank of every value in a numeric Series. A value's percentile rank is its rank (1 for the smallest value) divided by the number of values.

Requirements:

  • Return a float Series with values in (0, 1] . When the largest value is unique, it maps to 1.0 .
  • Preserve the original index of s .
  • Ties share the average of their ranks ( method='average' ).

Example:

s = pd.Series([10, 20, 20, 40])
percentile_rank(s)
# 0    0.250
# 1    0.625   # tie: avg of ranks 2 and 3 -> 2.5/4
# 2    0.625
# 3    1.000
# dtype: float64

The two 20s would take ranks 2 and 3, so each gets the average 2.5, and 2.5 / 4 = 0.625.

What a Strong Answer Covers Guidance

  • Ranking in ascending order and dividing by the count of non-missing values
  • Average tie handling, checked against the tied values in the example
  • What the largest value maps to when it is tied
  • Keeping the original index and returning floats
  • How missing values are treated

Follow-up Questions Guidance

  • How would you rank in descending order instead?
  • What happens to NaN values, and how would you control that?
  • If the largest value appears twice, what does it map to, and which tie method would make it 1.0 ?
  • How would you compute the percentile rank within each group of another column?