Python
All Top 50 Python & Pandas Interview Questions#33
Reshape Data from Wide to Long Format with melt() in Pandas
MediumGoogleInterview Question #33
Asked at GoogleGiven a wide financial budget DataFrame with columns `['department', '2026-01', '2026-02', '2026-03']`, unpivot it into a tidy long DataFrame with columns `['department', 'month', 'budget']`.
Input Table: wide_budget
2 rows preview| department | 2026-01 | 2026-02 |
|---|---|---|
| Marketing | 50000 | 60000 |
| Engineering | 120000 | 130000 |
Expected Output Structure4 rows
| department | month | budget |
|---|---|---|
| Marketing | 2026-01 | 50000 |
| Marketing | 2026-02 | 60000 |
| Engineering | 2026-01 | 120000 |
| Engineering | 2026-02 | 130000 |
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.