Pivot Sales by Region and Quarter
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
| region | quarter | revenue |
|---|---|---|
| North | Q1 | 100 |
| North | Q2 | 200 |
| North | Q3 | 150 |
| South | Q1 | 80 |
| South | Q2 | 90 |
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 salesSolve 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