Sort a Pandas DataFrame by Score Descending While Keeping Tie Order Stable
Quick Overview
Implement a pandas function that sorts a DataFrame of names and scores by score in descending order while rows with equal scores keep their input order. It tests stable sorting in sort_values, keeping the original index labels, and returning a new DataFrame without mutating the input.
Sort a Pandas DataFrame by Score Descending While Keeping Tie Order Stable
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def sort_by_score(df: pd.DataFrame) -> pd.DataFrame` for a DataFrame with columns `'name'` (str) and `'score'` (int). Return a new DataFrame sorted by `'score'` descending.
The function must meet three requirements:
- Equal scores keep original input order: use `df.sort_values('score', ascending=False, kind='stable')`.
- Do not reset the index (keep original index labels).
- Do not mutate the input `df`.
Example:
```python
df = pd.DataFrame(
{"name": ["a", "b", "c", "d"], "score": [10, 30, 10, 30]},
index=[0, 1, 2, 3],
)
sort_by_score(df)
# name score
# 1 b 30
# 3 d 30
# 0 a 10
# 2 c 10
```
```hint Ties
Rows `b` and `d` share a score, and so do `a` and `c`. Ask what decides their order in the output, and what the default sorting algorithm promises about equal keys.
```
### What a Strong Answer Covers
- Sorts on `score` in descending order with `sort_values`.
- Explains why ties need a stable algorithm and what the default algorithm does with them.
- Keeps the original index labels and returns a new DataFrame, leaving `df` unchanged.
- States the time and space cost of the sort.
### Follow-up Questions
- Why does `kind='stable'` matter here, and what is the default?
- How would you sort by `'score'` descending but `'name'` ascending?
- What changes if `score` can contain missing values?
Overview: Implement a pandas function that sorts a DataFrame of names and scores by score in descending order while rows with equal scores keep their input order. It tests stable sorting in sort_values, keeping the original index labels, and returning a new DataFrame without mutating the input.
Implement def sort_by_score(df: pd.DataFrame) -> pd.DataFrame for a DataFrame with columns 'name' (str) and 'score' (int). Return a new DataFrame sorted by 'score' descending.
The function must meet three requirements:
Equal scores keep original input order: use
df.sort_values('score', ascending=False, kind='stable')
.
Do not reset the index (keep original index labels).
Do not mutate the input
df
.
Example:
df = pd.DataFrame(
{"name": ["a", "b", "c", "d"], "score": [10, 30, 10, 30]},
index=[0, 1, 2, 3],
)
sort_by_score(df)
# name score
# 1 b 30
# 3 d 30
# 0 a 10
# 2 c 10
What a Strong Answer Covers Guidance
Sorts on
score
in descending order with
sort_values
.
Explains why ties need a stable algorithm and what the default algorithm does with them.
Keeps the original index labels and returns a new DataFrame, leaving
df
unchanged.
States the time and space cost of the sort.
Follow-up Questions Guidance
Why does
kind='stable'
matter here, and what is the default?
How would you sort by
'score'
descending but
'name'
ascending?