Value lost to cancellations
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
Operations asks how much order value is lost to cancellations each month. Return one row per month with the columns `month` (text like `2024-01`) and `cancelled_share`, the cancelled amount divided by the total amount ordered in that month. A month without cancellations shows 0.
What this exercise teaches
where keeps the amount on cancelled rows and writes 0 elsewhere, so summing that column per month gives the lost value next to the full total. Dividing the two gives the share. Computing the lost total from a filtered table instead leaves out the months without cancellations unless you put them back with fillna(0).
A hint if you get stuck
Make a helper column that holds the amount for cancelled orders and 0 for the rest. Then both totals come from one groupby.
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
- Best-selling product in each region
- 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
- Average days between orders
- Messy export: clean, then total
- New and returning customers per month
- A conversion funnel