Sort a Pandas Series Ascending With NaN Values Last
Quick Overview
Implement a pandas function that sorts a numeric Series in ascending order with every missing value placed at the end. It tests how sort_values handles NaN, keeping index labels attached to their values, and returning all values without dropping any.
Sort a Pandas Series Ascending With NaN Values Last
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def sort_scores_nulls_last(s: pd.Series) -> pd.Series` that sorts a numeric Series ascending with all `NaN` values at the end, and returns the result.
The function must meet three requirements:
- Use `sort_values` with `na_position='last'` stated explicitly.
- Keep original index labels attached to their values.
- Do not drop `NaN` values.
Example:
```python
s = pd.Series([3.0, np.nan, 1.0, 2.0], index=['a', 'b', 'c', 'd'])
sort_scores_nulls_last(s)
# c 1.0
# d 2.0
# a 3.0
# b NaN
# dtype: float64
```
```hint Missing values
Before reaching for `dropna()` or a plain Python sort, check how `sort_values` treats missing values: whether it removes them, and which argument decides where they end up.
```
### What a Strong Answer Covers
- Sorts ascending with `sort_values` and passes `na_position='last'` explicitly.
- Keeps each value attached to its original label, with no `reset_index()` and no `ignore_index=True`.
- Keeps every `NaN`, so the output has the same length as the input.
- Considers edge cases such as an empty Series, an all-`NaN` Series and tied scores.
### Follow-up Questions
- How would you sort descending while still keeping the `NaN` values at the end?
- Two items have the same score. Which one comes first, and how would you make that order deterministic?
- How would you apply the same rule to a `score` column of a DataFrame, with a second column to break ties?
Overview: Implement a pandas function that sorts a numeric Series in ascending order with every missing value placed at the end. It tests how sort_values handles NaN, keeping index labels attached to their values, and returning all values without dropping any.
Implement def sort_scores_nulls_last(s: pd.Series) -> pd.Series that sorts a numeric Series ascending with all NaN values at the end, and returns the result.
The function must meet three requirements:
Use
sort_values
with
na_position='last'
stated explicitly.
Keep original index labels attached to their values.
Do not drop
NaN
values.
Example:
s = pd.Series([3.0, np.nan, 1.0, 2.0], index=['a', 'b', 'c', 'd'])
sort_scores_nulls_last(s)
# c 1.0
# d 2.0
# a 3.0
# b NaN
# dtype: float64
What a Strong Answer Covers Guidance
Sorts ascending with
sort_values
and passes
na_position='last'
explicitly.
Keeps each value attached to its original label, with no
reset_index()
and no
ignore_index=True
.
Keeps every
NaN
, so the output has the same length as the input.
Considers edge cases such as an empty Series, an all-
NaN
Series and tied scores.
Follow-up Questions Guidance
How would you sort descending while still keeping the
NaN
values at the end?
Two items have the same score. Which one comes first, and how would you make that order deterministic?
How would you apply the same rule to a
score
column of a DataFrame, with a second column to break ties?