Pandasdrill

Level 2: Cleaning Exercise 28 of 102

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 exercise

More exercises in Cleaning