Pandasdrill

Level 2: Cleaning Exercise 21 of 63 Pro

Clean messy money text

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

The `amount` column holds text such as "$1,250.00", " $89.50" and "1,000", with stray spaces, dollar signs and thousands separators. Return it as a Series of floats.

What this exercise teaches

A character class like [$,\s] matches a dollar sign, a comma or any whitespace, and replacing all of them with nothing leaves a clean numeric string. astype(float) then converts. Forgetting the comma leaves "1,000", which float() cannot parse, and skipping the conversion leaves text that looks like numbers but cannot be summed.

A hint if you get stuck

Strip every character that is not part of a number with str.replace and a regular expression (regex=True), then convert with astype(float) or pd.to_numeric.

Try it now, free, no account needed

The first 15 tasks 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 adds timed mode and the certificate.

Open this exercise

More exercises in Cleaning