Pandasdrill

Level 7: Interview cases Exercise 98 of 102

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 exercise

More exercises in Interview cases