Sort a Pandas DataFrame by Multiple Columns with Mixed Order
Quick Overview
Write a pandas function that sorts an employee DataFrame by department ascending and salary descending, resets the index, and leaves the input unchanged. It tests per-column sort directions, index handling, and how to make tied rows come out in a deterministic order.
Sort a Pandas DataFrame by Multiple Columns with Mixed Order
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `def sort_multi(df: pd.DataFrame) -> pd.DataFrame` for a DataFrame with the columns `dept` (str), `salary` (int) and `name` (str). Sort by `dept` ascending, then by `salary` descending.
The function must meet these requirements:
- Sort by `['dept', 'salary']` with `ascending=[True, False]`.
- Reset the result to a fresh `0..n-1` `RangeIndex` with `reset_index(drop=True)`.
- Leave the input unchanged and return a new DataFrame.
Example:
```python
df = pd.DataFrame({
'dept': ['B', 'A', 'A'],
'salary': [100, 90, 120],
'name': ['x', 'y', 'z'],
})
sort_multi(df)
# dept salary name
# 0 A 120 z
# 1 A 90 y
# 2 B 100 x
```
```hint Per-column direction
Each sort column needs its own direction. Check which types the `ascending` argument of `sort_values` accepts, and look at the index labels the rows carry after sorting.
```
### What a Strong Answer Covers
- Giving `dept` and `salary` their own sort directions in a single sort call.
- Why the order of columns in `by` decides which column breaks ties for the other.
- Replacing the shuffled index labels with a fresh `RangeIndex`.
- Returning a new DataFrame without modifying the caller's frame.
- The time and space cost of the sort.
### Follow-up Questions
- How would you keep ties stable and deterministic?
- How would you sort `dept` case-insensitively while keeping the same directions?
- If `salary` had missing values, where would those rows end up, and how would you move them?
Overview: Write a pandas function that sorts an employee DataFrame by department ascending and salary descending, resets the index, and leaves the input unchanged. It tests per-column sort directions, index handling, and how to make tied rows come out in a deterministic order.
Implement def sort_multi(df: pd.DataFrame) -> pd.DataFrame for a DataFrame with the columns dept (str), salary (int) and name (str). Sort by dept ascending, then by salary descending.
The function must meet these requirements:
Sort by
['dept', 'salary']
with
ascending=[True, False]
.
Reset the result to a fresh
0..n-1RangeIndex
with
reset_index(drop=True)
.
Leave the input unchanged and return a new DataFrame.
Example:
df = pd.DataFrame({
'dept': ['B', 'A', 'A'],
'salary': [100, 90, 120],
'name': ['x', 'y', 'z'],
})
sort_multi(df)
# dept salary name
# 0 A 120 z
# 1 A 90 y
# 2 B 100 x
What a Strong Answer Covers Guidance
Giving
dept
and
salary
their own sort directions in a single sort call.
Why the order of columns in
by
decides which column breaks ties for the other.
Replacing the shuffled index labels with a fresh
RangeIndex
.
Returning a new DataFrame without modifying the caller's frame.
The time and space cost of the sort.
Follow-up Questions Guidance
How would you keep ties stable and deterministic?
How would you sort
dept
case-insensitively while keeping the same directions?
If
salary
had missing values, where would those rows end up, and how would you move them?