Python
All Top 50 Python & Pandas Interview Questions#14
Flatten MultiIndex Groupby Results with reset_index() in Pandas
EasyInterview Question #14
After running `df.groupby(['region', 'category'])['revenue'].sum()`, the resulting Series has a MultiIndex. Convert it back into a flat DataFrame with clean column headers.
Input Table: multi_index_series
2 rows preview| region | category | revenue |
|---|---|---|
| North | Electronics | 45000 |
| North | Fashion | 32000 |
Expected Output Structure2 rows
| region | category | total_revenue |
|---|---|---|
| North | Electronics | 45000 |
| North | Fashion | 32000 |
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.