55. Unpivot Wide Columns to Rows
Difficulty: Medium · Topics: Pivot, Unpivot & Rollup, Null Handling, Filtering & Selection
The budget table arrived in spreadsheet shape: one column per month (jan, feb, mar).
Turn it into a long table with columns dept, month (the column name, e.g. 'jan') and amount. Leave out months with no amount (null).
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
budget
| dept | jan | feb | mar |
|---|---|---|---|
| Sales | 100 | 120 | 90 |
| IT | 300 | null | 310 |
Expected output
| dept | month | amount |
|---|---|---|
| Sales | jan | 100 |
| Sales | feb | 120 |
| Sales | mar | 90 |
| IT | jan | 300 |
| IT | mar | 310 |
Hints
Hint 1
Spark 3.4+ hasdf.unpivot(ids, values, variableColumnName, valueColumnName) (alias melt).Hint 2
The ids stay as they are; each value column becomes one row per input row.Hint 3
Unpivot keeps nulls, so filter them afterwards.Learn the concepts
- unpivot · 3 min read. Turn columns into rows (wide to long) with unpivot or the SQL stack() function.
- filter / where · 5 min read. Keep the rows that match a condition, and the null and operator traps that silently drop data.
PySpark functions you'll practise
- unpivot
- isNotNull
- filter
Related problems
- Survey Scores by Question · Medium · Pivot, Unpivot & Rollup, Aggregations
- Pivot Quarterly Revenue · Medium · Pivot, Unpivot & Rollup, Aggregations
- Subtotals with ROLLUP · Hard · Pivot, Unpivot & Rollup, Aggregations, Conditional Logic, Filtering & Selection
- All Combinations with CUBE · Hard · Pivot, Unpivot & Rollup, Aggregations, Conditional Logic
- Pivot with Several Aggregations · Hard · Pivot, Unpivot & Rollup, Aggregations, Null Handling
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice