Python
All Top Python 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: df
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
Starting the real Python + pandas runtime (one-time download)…