Product Category Pivot

Medium
Pivoting
Aggregation

Product Category Pivot

The sales table records quarterly revenue by category.

Pivot the quarters into columns 1, 2, 3, 4 and return category, 1, 2, 3, 4 with summed revenue in each quarter. Sort by category ascending.

Browser Engine: Evaluates PySpark DataFrame & SQL logic locally in Pyodide WebAssembly.

Input data

1 DataFrame

These DataFrames are passed to solve(spark, dfs). Access one with dfs['name'].

df.sales12 rows
sale_id (int)category (string)quarter (int)amount (double)
1Audio1100
2Audio2120
3Audio390
4Audio4110
5Home180
6Home270
7Home360
8Home450
9Office140
10Office245
11Office350
12Office455

Return these columns

category(string)
1(double)
2(double)
3(double)
4(double)

Column names and order must match. Sort order is specified in the question.

Browser engine
Loading Python editor…
Click Run to try the sample cases, or Submit to check the hidden cases. First run caches the browser Python engine (~8–15s once); all code stays on your device.