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 exerciseMore exercises in Cleaning
- Fill missing values in one columnPro
- Drop rows missing a key fieldPro
- Convert text to numbersPro
- Tidy up text with strip and lowerPro
- Search text with str.containsPro
- Pull numbers out of codesPro
- Remove duplicate recordsPro
- Rename columns and replace a placeholderPro
- Dates with to_datetime and dtPro
- Split a column into twoPro
- Fill gaps with interpolatePro
- Parse dates in day/month/year orderPro