Rank a pandas Series like SQL ROW_NUMBER with method='first'
Quick Overview
Rank the values of a numeric pandas Series so every element gets a distinct rank starting at 1, with ties broken by position as SQL ROW_NUMBER does. Tests pandas rank tie methods, index preservation, missing values and ranking within groups.
Rank a pandas Series like SQL ROW_NUMBER with method='first'
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def ordinal_rank(s: pd.Series) -> pd.Series` that ranks each element of a numeric Series ascending using `s.rank(method='first')` (equivalent to SQL `ROW_NUMBER`).
Requirements:
- Break ties by original position: the element that appears earlier in the Series gets the smaller rank.
- Every element gets a distinct integer-valued rank starting at 1.
- Preserve the original index. Result dtype is `float`.
Example:
```python
s = pd.Series([40, 10, 40, 20], index=['a', 'b', 'c', 'd'])
ordinal_rank(s)
# a 3.0
# b 1.0
# c 4.0
# d 2.0
# dtype: float64
```
```hint Tie order
Work out whether ties are broken by position in the Series or by index label, and check what `rank` does with missing values by default.
```
### What a Strong Answer Covers
- How `method='first'` resolves ties, and why every rank ends up distinct.
- That the original index is kept and the result is float.
- How `'first'` compares with the other tie methods (`'average'`, `'min'`, `'max'`, `'dense'`).
- What missing values do to the distinct-rank guarantee.
### Follow-up Questions
- How does `method='first'` differ from `'min'` and `'dense'`?
- How would you replicate SQL `ROW_NUMBER() OVER (PARTITION BY g ORDER BY v)`?
- How would you handle missing values if every row, including those with a missing value, must get a rank?
Overview: Rank the values of a numeric pandas Series so every element gets a distinct rank starting at 1, with ties broken by position as SQL ROW_NUMBER does. Tests pandas rank tie methods, index preservation, missing values and ranking within groups.
Implement def ordinal_rank(s: pd.Series) -> pd.Series that ranks each element of a numeric Series ascending using s.rank(method='first') (equivalent to SQL ROW_NUMBER).
Requirements:
Break ties by original position: the element that appears earlier in the Series gets the smaller rank.
Every element gets a distinct integer-valued rank starting at 1.
Preserve the original index. Result dtype is
float
.
Example:
s = pd.Series([40, 10, 40, 20], index=['a', 'b', 'c', 'd'])
ordinal_rank(s)
# a 3.0
# b 1.0
# c 4.0
# d 2.0
# dtype: float64
What a Strong Answer Covers Guidance
How
method='first'
resolves ties, and why every rank ends up distinct.
That the original index is kept and the result is float.
How
'first'
compares with the other tie methods (
'average'
,
'min'
,
'max'
,
'dense'
).
What missing values do to the distinct-rank guarantee.
Follow-up Questions Guidance
How does
method='first'
differ from
'min'
and
'dense'
?
How would you replicate SQL
ROW_NUMBER() OVER (PARTITION BY g ORDER BY v)
?
How would you handle missing values if every row, including those with a missing value, must get a rank?