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.
Module 5 · about 15 minutes · uses Lesson 5.2. You’ll be able to translate codes into names and recode values quickly.
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
2. map with a Series: VLOOKUP
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
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:
| order | store_id |
|---|---|
| 1 | S01 |
| 2 | S03 |
| 3 | S07 |
city = {'S01': 'Toronto', 'S03': 'Halifax'}df['store_id'].map(city)| order | store_id | city |
|---|---|---|
| 1 | S01 | Toronto |
| 2 | S03 | Halifax |
| 3 | S07 | NaN |
df.loc[df['city'].isna(), 'store_id'].unique() → ['S07']: add it to the lookup.4. Your turn
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
- Map
segmentcodes{'R': 'Regular', 'B': 'Bargain', 'P': 'Premium'}. - How do you find values that
mapdidn’t translate? - You need a product’s name, category and cost on each order line. map or merge?
Answers
df['segment'] = df['code'].map({'R': 'Regular', 'B': 'Bargain', 'P': 'Premium'}).df.loc[df['new'].isna(), 'old'].unique().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
Sources
- pandas documentation, User Guide: pandas.Series.map https://pandas.pydata.org/docs/reference/api/pandas.Series.map.html
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
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.