Return the top N rows of a pandas DataFrame by revenue, breaking ties by first occurrence
Quick Overview
Write a pandas function that returns the n rows of a DataFrame with the highest revenue, sorted from largest to smallest. It tests top-n selection, tie handling at the cutoff, keeping the original index labels, and leaving the input frame unchanged.
Return the top N rows of a pandas DataFrame by revenue, breaking ties by first occurrence
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write a pandas function that takes a DataFrame with an integer column `'revenue'` and returns the `n` rows with the largest revenue, ordered from largest to smallest.
```python
import pandas as pd
def top_n_by_value(df: pd.DataFrame, n: int) -> pd.DataFrame:
...
```
The function must:
- Keep the rows that occur first in `df` when several rows tie on revenue at the nth position.
- Preserve the original index labels of the selected rows.
- Leave the input `df` unchanged.
Example:
```python
df = pd.DataFrame({'revenue': [10, 30, 20, 30]}, index=['a', 'b', 'c', 'd'])
top_n_by_value(df, 2)
# revenue
# b 30
# d 30
top_n_by_value(df, 1)
# revenue
# b 30
```
With `n = 2` both rows with revenue 30 fit. With `n = 1`, rows `b` and `d` tie for the single slot, and `b` is kept because it comes first.
```hint Where to look
pandas has a method built for top-n selection. Check what its tie argument does at the cutoff. If you sort instead, check whether the sort keeps tied rows in their original order.
```
### Constraints & Assumptions
- `'revenue'` has an integer dtype.
- The index can hold arbitrary labels, such as the strings in the example, and the result must keep them.
- `df` may have other columns; return whole rows.
### What a Strong Answer Covers
- A result of at most `n` rows ordered by revenue from largest to smallest.
- A tie rule at the cutoff that keeps the earlier rows, with a reason the chosen method guarantees it.
- Index labels preserved and the input frame left unmodified.
- The cost of the approach compared with sorting the whole frame.
- Behavior when `n` is zero or larger than the number of rows.
### Follow-up Questions
- How would you return every row tied with the nth value, even if that gives more than `n` rows?
- How would you return the top `n` rows by revenue within each region?
- If `revenue` were a float column with missing values, where would those rows end up, and what should the function do about them?
- Why can `df.sort_values('revenue', ascending=False).head(n)` break the tie rule, and how would you fix it?
Overview: Write a pandas function that returns the n rows of a DataFrame with the highest revenue, sorted from largest to smallest. It tests top-n selection, tie handling at the cutoff, keeping the original index labels, and leaving the input frame unchanged.
Write a pandas function that takes a DataFrame with an integer column 'revenue' and returns the n rows with the largest revenue, ordered from largest to smallest.