AnalystPath

Pivot Sales by Region and Quarter

PandasMediumMid levelWalmart~15 min

Problem

**[Asked at Walmart]**

Pivot the `sales` DataFrame so each region is one row with columns Q1, Q2, Q3, Q4.
Fill missing values with 0.

Return columns: `region`, `Q1`, `Q2`, `Q3`, `Q4`. Order by region.

Input data

Example rows — the live problem includes the full dataset.

sales
regionquarterrevenue
NorthQ1100
NorthQ2200
NorthQ3150
SouthQ180
SouthQ290

Expected output

Your answer should return 3 rows with the columns region, Q1, Q2, Q3, Q4.

Starter code (Pandas (Python))

import pandas as pd

def pivot_sales(sales: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return sales

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions