Data Cleaning Exercises

Reference · Python (pandas)

Map values with a dictionary in Python (pandas)

Replace each spelling with its approved value using a reviewed dictionary, and decide what happens to unmapped values.

When to use it

Categories with many spellings for one meaning.

How

Series.map with a function that returns None for unknown values.

s.map(lambda v: DICTIONARY.get(v) if isinstance(v, str) else None)
  • Series.map(dict) also returns missing for unmapped keys; Series.replace keeps them unchanged.

Pitfalls

  • Keeping unmapped values unchanged.
  • Mapping before normalizing.
  • Guessing mappings for unseen spellings.

Lesson: Map categories with an approved dictionary

Practise it: Map channels with an approved dictionary