Unstack the quarter level of a pandas MultiIndex into columns
Quick Overview
Pivot a pandas DataFrame indexed by store and quarter into a wide table with one column per quarter. It tests how unstack reshapes a MultiIndex, how to flatten the resulting column header, and how missing store and quarter pairs affect values and dtypes.
Unstack the quarter level of a pandas MultiIndex into columns
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
You have a pandas DataFrame `df` with a 2-level `MultiIndex` (`'store'`, `'quarter'`) and one column, `'revenue'`. Implement `unstack_last_level(df)` so that it pivots `'quarter'` into columns and returns a wide table indexed by `'store'`, with one column per quarter label.
The function must:
- Unstack with `df.unstack('quarter')`.
- Drop the top column level: `result.columns = result.columns.droplevel(0)`.
- Clear the columns axis name with `rename_axis(columns=None)`.
- Keep `'store'` as the index (no `reset_index`).
- Leave missing store/quarter combinations as `NaN`.
For example:
```python
# Input (MultiIndex: store, quarter)
# revenue
# store quarter
# A Q1 100
# Q2 150
# B Q1 200
result = unstack_last_level(df)
# Q1 Q2
# store
# A 100.0 150.0
# B 200.0 NaN
```
```hint Inspect the intermediate result
Print `df.unstack('quarter').columns` and `.dtypes` before changing anything. Count the header levels and compare the dtype with the input's.
```
### Constraints & Assumptions
- `df` has exactly one value column, `'revenue'`, holding numbers.
- Each (store, quarter) pair appears at most once in the index.
- Quarter labels are strings such as `'Q1'` to `'Q4'`, and not every store has a row for every quarter.
### What a Strong Answer Covers
- Moving the `'quarter'` level into the columns while `'store'` stays the row index.
- Why the unstacked header has two levels, and which level to drop so only the quarter labels remain.
- Clearing the leftover columns axis name.
- How missing combinations become `NaN` and what that does to the column dtype.
### Follow-up Questions
- How does `unstack` differ from `pivot` here?
- How would you fill the `NaN` gaps with 0 instead, and what happens to the dtype?
- What happens if the same store and quarter appear twice in the index, and how would you handle it?
Overview: Pivot a pandas DataFrame indexed by store and quarter into a wide table with one column per quarter. It tests how unstack reshapes a MultiIndex, how to flatten the resulting column header, and how missing store and quarter pairs affect values and dtypes.
You have a pandas DataFrame df with a 2-level MultiIndex ('store', 'quarter') and one column, 'revenue'. Implement unstack_last_level(df) so that it pivots 'quarter' into columns and returns a wide table indexed by 'store', with one column per quarter label.
The function must:
Unstack with
df.unstack('quarter')
.
Drop the top column level:
result.columns = result.columns.droplevel(0)
.
Clear the columns axis name with
rename_axis(columns=None)
.
Keep
'store'
as the index (no
reset_index
).
Leave missing store/quarter combinations as
NaN
.
For example:
# Input (MultiIndex: store, quarter)
# revenue
# store quarter
# A Q1 100
# Q2 150
# B Q1 200
result = unstack_last_level(df)
# Q1 Q2
# store
# A 100.0 150.0
# B 200.0 NaN
Constraints & Assumptions
df
has exactly one value column,
'revenue'
, holding numbers.
Each (store, quarter) pair appears at most once in the index.
Quarter labels are strings such as
'Q1'
to
'Q4'
, and not every store has a row for every quarter.
What a Strong Answer Covers Guidance
Moving the
'quarter'
level into the columns while
'store'
stays the row index.
Why the unstacked header has two levels, and which level to drop so only the quarter labels remain.
Clearing the leftover columns axis name.
How missing combinations become
NaN
and what that does to the column dtype.
Follow-up Questions Guidance
How does
unstack
differ from
pivot
here?
How would you fill the
NaN
gaps with 0 instead, and what happens to the dtype?
What happens if the same store and quarter appear twice in the index, and how would you handle it?