Recode values with map
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
Add a column `score` to `tickets` that turns the priority into a number: low is 1, medium is 2 and high is 3. A priority that is not one of those three gets NaN. Return the whole DataFrame with the new column.
What this exercise teaches
map replaces each value by its dictionary entry and gives NaN when there is none, which makes unexpected categories visible. replace with the same dictionary leaves unknown values as they are, so the column would end up with numbers and text mixed.
A hint if you get stuck
Series.map takes a dictionary and looks every value up in it. Values that are not in the dictionary become NaN.
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 Cleaning
- Fill missing values in one column
- Drop rows missing a key field
- Convert text to numbers
- Tidy up text with strip and lower
- Search text with str.contains
- Pull numbers out of codes
- Remove duplicate records
- Rename columns and replace a placeholder
- Dates with to_datetime and dt
- Clean messy money text
- Split a column into two
- Fill gaps with interpolate
- Parse dates in day/month/year order
- Text to numbers, bad values to NaN
- Limit values to a range with clip