All lessons

Module 5: Combining tables · Lesson 5.4 · about 15 min

Lookups with map and replace

Translate values with map (from a dictionary or a Series, like a VLOOKUP), recode labels with replace, and know when a merge is the better tool.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 5 · about 15 minutes · uses Lesson 5.2. You’ll be able to translate codes into names and recode values quickly.

map translates each value through a lookup, like VLOOKUP for one column; replace swaps only the values you list and leaves the rest alone.

In everyday terms: a phrasebook: look up each word and write its translation. Words not in the book come back blank with map, or unchanged with replace.

1. map with a dictionary

Pythonorders.csv

2. map with a Series: VLOOKUP

Pythonorders.csv, products.csv

If the lookup’s key repeats, map raises an error (it can’t choose), which protects you from the duplicate-key trap in Lesson 5.3.

3. replace: recode some values, keep the rest

Python

replace changes only the values you list. map turns anything you don’t list into NaN, which makes it the stricter choice for lookups.

s.map(dict)
Each value becomes dict[value]; values not in the dict become NaN.
s.map(series)
Uses the Series index as keys: a VLOOKUP from another table.
s.replace({...})
Changes only listed values; others stay as they are.
merge instead
Better when you need several columns from the lookup table.
Physical meaning
map keeps the row count fixed (one answer per row), which makes it safer than a join for a single column. A NaN after map is a missing entry in your lookup, not missing data.

“map and replace are the same.” map with a dictionary turns every value not in it into NaN; replace leaves those values untouched. Use replace to fix a few labels, map to translate all of them.

Watch it happen, one step at a time:

Codes.
orderstore_id
1S01
2S03
3S07
Lookup. city = {'S01': 'Toronto', 'S03': 'Halifax'}
map. df['store_id'].map(city)
orderstore_idcity
1S01Toronto
2S03Halifax
3S07NaN
replace instead. S07 would stay as 'S07'.
Find gaps. df.loc[df['city'].isna(), 'store_id'].unique() → ['S07']: add it to the lookup.

4. Your turn

Your turncustomers.csv, stores.csv
How it works

Principle: map looks each value up in a table of key → value (a dictionary or a Series index) and returns what it finds. It’s a left join that brings back exactly one column, with a guarantee of one match per row.

Tool Brings Unmatched values
map one column, one key NaN
replace recoded values unchanged
merge any columns, any keys NaN (left join)

5. Practice

  1. Map segment codes {'R': 'Regular', 'B': 'Bargain', 'P': 'Premium'}.
  2. How do you find values that map didn’t translate?
  3. You need a product’s name, category and cost on each order line. map or merge?
Answers
  1. df['segment'] = df['code'].map({'R': 'Regular', 'B': 'Bargain', 'P': 'Premium'}).
  2. df.loc[df['new'].isna(), 'old'].unique().
  3. merge, because you need three columns.

New words: map, lookup table, VLOOKUP, replace, set_index.

  • map translates every value; missing keys give NaN.
  • map with a Series works like VLOOKUP from another table.
  • replace recodes only the values you list.
  • Use merge when you need several columns from the lookup.

Check yourself

1What does s.map({'S01': 'Harbourview'}) give for 'S02'?
2Which is the pandas version of a VLOOKUP for one column?
3When is merge better than map?

Sources

Your ticks stay in this browser. Save them as a file on the course home.

Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.