Best-selling product in each region
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
A regional manager asks: in each region, which product brought in the most revenue in total? Return one row per region with the columns `region`, `product` and `revenue`, where revenue is that product's total in the region.
What this exercise teaches
The first groupby builds the totals, and idxmax on those totals finds the winning row per region. Skipping the first step answers a different question: it returns the single largest order in each region, which can belong to a product that sells less overall.
A hint if you get stuck
Two rounds of grouping: first the total per region and product, then the best row within each region.
Try it now, free, no account needed
Your first 8 exercises on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro unlocks every exercise, timed mode and the certificate.
Open this exerciseMore exercises in Interview cases
- Revenue by country
- Repeat customers
- Month over month revenue growth
- Customers who never ordered
- Average order value by segment
- New customers per month
- Second order within 30 days
- A join that inflates the total
- Top three customers and their share
- Value lost to cancellations
- Average days between orders
- Messy export: clean, then total
- New and returning customers per month
- A conversion funnel