Return the n highest and n lowest rows of a DataFrame in one tagged table
Quick Overview
Write a pandas function that returns the n highest and n lowest rows of a DataFrame by one numeric column in a single table tagged high or low. It tests NaN exclusion, a deterministic index tie-break in both sort directions, and keeping the original index and columns intact.
Return the n highest and n lowest rows of a DataFrame in one tagged table
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `extreme_rows(df, col, n)` that returns the `n` largest and `n` smallest rows of `df` by numeric column `col` in one table, with an added column `extreme_type` (`'high'` or `'low'`).
The result must follow these rules:
- Keep every original column of `df` plus `extreme_type`.
- Order: all `'high'` rows by `col` descending, then all `'low'` rows by `col` ascending.
- Ties within either block: original index ascending.
- Preserve original index labels (do not reset).
- Exclude rows with `NaN` in `col` from both ends.
Example:
```text
df:
index name value
0 a 10
1 b 99
2 c 3
3 d 50
extreme_rows(df, 'value', 1)
index name value extreme_type
1 b 99 high
2 c 3 low
```
```hint Ties in both directions
Equal values in `col` must come out in index order in both blocks, including the descending one. Think about what a stable sort guarantees and what order the rows are in before you sort.
```
### What a Strong Answer Covers
- Excluding rows with `NaN` in `col` before either end is selected.
- A deterministic tie-break by original index that holds in the descending block as well as the ascending one.
- Tagging each block and concatenating high before low without resetting the index or modifying `df`.
- Time and space complexity, and edge cases such as `n = 0` or a column with no non-null values.
### Follow-up Questions
- When can the high and low blocks share rows, and what policy would you choose for that case?
- How would you write this with `nlargest` and `nsmallest`, and what would you need to check about their tie handling?
- How would you produce the same table in SQL with a guaranteed final order?
Overview: Write a pandas function that returns the n highest and n lowest rows of a DataFrame by one numeric column in a single table tagged high or low. It tests NaN exclusion, a deterministic index tie-break in both sort directions, and keeping the original index and columns intact.
Write extreme_rows(df, col, n) that returns the n largest and n smallest rows of df by numeric column col in one table, with an added column extreme_type ('high' or 'low').
The result must follow these rules:
Keep every original column of
df
plus
extreme_type
.
Order: all
'high'
rows by
col
descending, then all
'low'
rows by
col
ascending.
Ties within either block: original index ascending.
Preserve original index labels (do not reset).
Exclude rows with
NaN
in
col
from both ends.
Example:
df:
index name value
0 a 10
1 b 99
2 c 3
3 d 50
extreme_rows(df, 'value', 1)
index name value extreme_type
1 b 99 high
2 c 3 low
What a Strong Answer Covers Guidance
Excluding rows with
NaN
in
col
before either end is selected.
A deterministic tie-break by original index that holds in the descending block as well as the ascending one.
Tagging each block and concatenating high before low without resetting the index or modifying
df
.
Time and space complexity, and edge cases such as
n = 0
or a column with no non-null values.
Follow-up Questions Guidance
When can the high and low blocks share rows, and what policy would you choose for that case?
How would you write this with
nlargest
and
nsmallest
, and what would you need to check about their tie handling?
How would you produce the same table in SQL with a guaranteed final order?