Back to Practice
#0081

Pivot Sales by Region and Product

MediumPandas20 min20 XP

Problem

Given a DataFrame with region, product, and revenue, return a pivot table with region as rows, product as columns, summed revenue as values, missing combinations filled with 0, sorted by region.

Why This Matters

Pivot tables turn event rows into comparison tables for reports, dashboards, and feature matrices.

Function Signature

def pivot_sales(df):

Examples

Example 1
Inputregion/product/revenue rows
Outputmatrix of summed revenue

Each region-product pair becomes one cell.

Constraints

  • Use sum aggregation.
  • Fill missing cells with 0.
  • Sort rows by region.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Region product matrix
Inputsales rows
Expectedpivot table

Revenue is summed per region and product.

Hidden Test Categories
duplicate region-product rowsmissing combinationsrow sorting