Sort a DataFrame by low, medium, high priority using an ordered categorical
Quick Overview
Sort a pandas DataFrame by a priority column holding low, medium and high in their logical order while keeping the original index labels. Tests ordered categorical dtypes, stable sorting of tied rows, and matching an expected frame exactly.
Sort a DataFrame by low, medium, high priority using an ordered categorical
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `sort_by_priority(df: pd.DataFrame) -> pd.DataFrame`. The `'priority'` column holds strings from `{'low', 'medium', 'high'}`. Convert it to an ordered categorical and return the frame stably sorted ascending by `low < medium < high`.
Requirements:
- The returned `'priority'` column must have an ordered categorical dtype with categories `['low', 'medium', 'high']`, not `object`.
- The sort must be stable: rows with the same priority keep their original relative order.
- Keep the original index labels attached to their rows (no reset). Order, index and dtype must match the expected frame exactly under `assert_frame_equal`.
- Every value is a valid member of the category set.
Example:
```python
df = pd.DataFrame({'priority': ['high', 'low', 'medium']}, index=[10, 11, 12])
out = sort_by_priority(df)
# priority
# 11 low
# 12 medium
# 10 high
# out['priority'].dtype -> ordered CategoricalDtype
```
With ties, rows of the same priority stay in their input order:
```python
df = pd.DataFrame({'priority': ['high', 'low', 'high', 'low']}, index=[1, 2, 3, 4])
sort_by_priority(df)
# priority
# 2 low
# 4 low
# 1 high
# 3 high
```
```hint Check the default
Look up the default `kind` of `sort_values` and whether the library promises that it keeps tied rows in their original order.
```
### What a Strong Answer Covers
- Why sorting the raw strings gives the wrong order, and how the ordered dtype fixes it.
- A sort algorithm that is guaranteed stable, with a reason the default is not enough.
- Index labels that stay attached to their rows, and a returned column that keeps the categorical dtype.
### Follow-up Questions
- How would you sort descending (high first) instead?
- What happens if a value is not in the `categories` list?
- Why is a stable sort important here?
Overview: Sort a pandas DataFrame by a priority column holding low, medium and high in their logical order while keeping the original index labels. Tests ordered categorical dtypes, stable sorting of tied rows, and matching an expected frame exactly.
Implement sort_by_priority(df: pd.DataFrame) -> pd.DataFrame. The 'priority' column holds strings from {'low', 'medium', 'high'}. Convert it to an ordered categorical and return the frame stably sorted ascending by low < medium < high.
Requirements:
The returned
'priority'
column must have an ordered categorical dtype with categories
['low', 'medium', 'high']
, not
object
.
The sort must be stable: rows with the same priority keep their original relative order.
Keep the original index labels attached to their rows (no reset). Order, index and dtype must match the expected frame exactly under
assert_frame_equal
.
Every value is a valid member of the category set.
Example:
df = pd.DataFrame({'priority': ['high', 'low', 'medium']}, index=[10, 11, 12])
out = sort_by_priority(df)
# priority
# 11 low
# 12 medium
# 10 high
# out['priority'].dtype -> ordered CategoricalDtype
With ties, rows of the same priority stay in their input order: