Pandasdrill

Level 7: Interview cases Exercise 100 of 102

Messy export: clean, then total

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 colleague sends a raw export. Region names have stray spaces and mixed capitals, and amounts are text with dollar signs and thousands separators. Return the total amount per region with the columns `region` (lower case, no spaces around it) and `total` (a number), highest total first.

What this exercise teaches

Grouping on uncleaned names would treat East, east and " East" as three regions, and the text amounts cannot be summed as numbers at all. Cleaning first with the str methods and astype(float) turns the export into something a single groupby can total.

A hint if you get stuck

Fix both columns before grouping: strip and lower the names, remove the $ and the commas from the amounts and convert them to numbers.

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