Apply and Values
Map Values via Dictionary
Remap a column of categorical codes to labels using a dict lookup per row.
With pandas, Series.map(dict) does this in one call; keys absent from the
mapping become NaN rather than raising an error.
By hand
Loop over scores, look up each value in mapping, and append the label to
result. Every iteration shows both the current score s and the growing
result list.
naive.py
Replay: real traced execution (multi-file project)
scores = [1, 3, 2, 1, 3]
mapping = {1: 'low', 2: 'mid', 3: 'high'}
result = []
for s in scores:
result.append(mapping[s])
print('RESULT:', result)
scores ← [1, 3, 2, 1, 3]
1scores = [1, 3, 2, 1, 3]2mapping = {1: 'low', 2: 'mid', 3: 'high'}values this step[1, 3, 2, 1, 3]scoresmapping ← {1: 'low', 2: 'mid', 3: 'high'}
1scores = [1, 3, 2, 1, 3]2mapping = {1: 'low', 2: 'mid', 3: 'high'}3result = []values this step{1: 'low', 2: 'mid', 3: 'high'}mappingresult ← []
2mapping = {1: 'low', 2: 'mid', 3: 'high'}3result = []4for s in scores:values this step[]results ← 1
3result = []4for s in scores:5 result.append(mapping[s])values this step1sresult ← ['low']
4for s in scores:5 result.append(mapping[s])6print('RESULT:', result)values this step[] → ['low']results ← 3
3result = []4for s in scores:5 result.append(mapping[s])values this step1 → 3sresult ← ['low', 'high']
4for s in scores:5 result.append(mapping[s])6print('RESULT:', result)values this step['low'] → ['low', 'high']results ← 2
3result = []4for s in scores:5 result.append(mapping[s])values this step3 → 2sresult ← ['low', 'high', 'mid']
4for s in scores:5 result.append(mapping[s])6print('RESULT:', result)values this step['low', 'high'] → ['low', 'high', 'mid']results ← 1
3result = []4for s in scores:5 result.append(mapping[s])values this step2 → 1sresult ← ['low', 'high', 'mid', 'low']
4for s in scores:5 result.append(mapping[s])6print('RESULT:', result)values this step['low', 'high', 'mid'] → ['low', 'high', 'mid', 'low']results ← 3
3result = []4for s in scores:5 result.append(mapping[s])values this step1 → 3sresult ← ['low', 'high', 'mid', 'low', 'high']
4for s in scores:5 result.append(mapping[s])6print('RESULT:', result)values this step['low', 'high', 'mid', 'low'] → ['low', 'high', 'mid', 'low', 'high']resultfor s in scores:
3result = []4for s in scores:5 result.append(mapping[s])stdout ← RESULT: ['low', 'high', 'mid', 'low', 'high']
5 result.append(mapping[s])6print('RESULT:', result)values this stepRESULT: ['low', 'high', 'mid', 'low', 'high']stdout
With pandas
df['score'].map(mapping) replaces each element with the corresponding dict
value. The snapshot shows dtype: object because the mapped labels are
strings.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
scores = [1, 3, 2, 1, 3]
mapping = {1: 'low', 2: 'mid', 3: 'high'}
df = pd.DataFrame({'score': scores})
result = df['score'].map(mapping)
print('index:', result.index.tolist())
print('values:', result.tolist())
print('dtype:', result.dtype)
print('RESULT:', result.tolist())
index: [0, 1, 2, 3, 4]
values: ['low', 'high', 'mid', 'low', 'high']
dtype: object
RESULT: ['low', 'high', 'mid', 'low', 'high']
Implementation notes
- Keys not present in the mapping become
NaN(the original value is NOT preserved). To leave unmapped values unchanged, useSeries.replace(dict)instead. Series.mapalso accepts a function:df['score'].map(str)converts each element to a string. This is the same elementwise API asSeries.apply— each Python call runs per element.map-lookup-column(ch05) uses.mapfor a join-like lookup (one column's codes resolve foreign-key labels). The API is identical; the distinction is conceptual — here we remap existing column values, there we enrich from a separate lookup table.- Cross-reference:
dict-lookup-table(python-data-basics ch05) for the pure-Python dict-lookup version.