Pivot Sales Data to Show Monthly Totals by Product
PYSPARK coding challenge · Difficulty: easy · Topic: Pivot and Unpivot DataFrames · +50 XP
Problem
A retail company tracks sales with one row per product per month. Pivot it so each month becomes its own column, showing that product's total sales for the month.
Not every product sold in every month. Those gaps must read as 0, not as an empty cell.
Schema - `sales_data`
| Column | Type | | --- | --- | | product_id | int | | month | string (`YYYY-MM`) | | sales_amount | int |
Example Input - `sales_data`
| product_id | month | sales_amount | | --- | --- | --- | | 1 | 2024-01 | 100 | | 1 | 2024-02 | 150 | | 2 | 2024-01 | 200 | | 2 | 2024-02 | 250 | | 3 | 2024-03 | 300 | | 1 | 2024-03 | 130 |
Expected Output
| product_id | 2024-01 | 2024-02 | 2024-03 | | --- | --- | --- | --- | | 1 | 100 | 150 | 130 | | 2 | 200 | 250 | 0 | | 3 | 0 | 0 | 300 |
Explanation
The six input rows collapse into one row per product, with a column per distinct month.
Product 1 sold in all three months, so its row is filled. Product 2 never sold in 2024-03 and product 3 sold only in 2024-03 -- pivot leaves those combinations null, so .fillna(0) turns them into the zeros shown above. Without it the cells come back as NULL and the output does not match.
Notes
- The DataFrame is created for you - do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show() - Missing month/product combinations must be
0
What this PYSPARK challenge teaches you
“Pivot Sales Data to Show Monthly Totals by Product” is a easy-level PYSPARK challenge focused on Pivot and Unpivot DataFrames. Working through it gives you hands-on practice with pivot, groupBy, agg — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- pivot
- groupBy
- agg
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- 1 ≤ number of rows ≤ 100,000
- Each product_id appears in 1 to 12 months
- month is a string in 'YYYY-MM' format
- Output must have one row per product_id with columns for each month and product_id
- Use sum aggregation to calculate monthly totals
How to practise it on PySpark.in
Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYSPARK challenges
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
- Parse Apache Logs with Regex
- Optimize Small DataFrame Join with Broadcast
- Optimize Average Rating Calculation for Products
- Deduplicate and Aggregate User Actions with Latest Session
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Pivot and Unpivot DataFrames.