Python
All Top 50 Python & Pandas Interview Questions#37
Reshape Hierarchical Series with unstack() in Pandas
MediumInterview Question #37
Given a multi-index Series resulting from `df.groupby(['year', 'quarter'])['sales'].sum()`, pivot 'quarter' into column headers using `.unstack()`.
Input Table: multi_series
3 rows preview| year | quarter | sales |
|---|---|---|
| 2025 | Q1 | 100 |
| 2025 | Q2 | 150 |
| 2026 | Q1 | 120 |
Expected Output Structure2 rows
| year | Q1 | Q2 |
|---|---|---|
| 2025 | 100 | 150 |
| 2026 | 120 | NULL |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
Python 3.10 (Pandas)
Environment Ready
Click "Run & Test" to execute your Pandas code against the test assertions.