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)
  1. 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]scores
  2. mapping ← {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'}mapping
  3. result ← []

    2mapping = {1: 'low', 2: 'mid', 3: 'high'}3result = []4for s in scores:
    values this step[]result
  4. s ← 1

    3result = []4for s in scores:5    result.append(mapping[s])
    values this step1s
  5. result ← ['low']

    4for s in scores:5    result.append(mapping[s])6print('RESULT:', result)
    values this step[] ['low']result
  6. s ← 3

    3result = []4for s in scores:5    result.append(mapping[s])
    values this step1 3s
  7. result ← ['low', 'high']

    4for s in scores:5    result.append(mapping[s])6print('RESULT:', result)
    values this step['low'] ['low', 'high']result
  8. s ← 2

    3result = []4for s in scores:5    result.append(mapping[s])
    values this step3 2s
  9. result ← ['low', 'high', 'mid']

    4for s in scores:5    result.append(mapping[s])6print('RESULT:', result)
    values this step['low', 'high'] ['low', 'high', 'mid']result
  10. s ← 1

    3result = []4for s in scores:5    result.append(mapping[s])
    values this step2 1s
  11. result ← ['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']result
  12. s ← 3

    3result = []4for s in scores:5    result.append(mapping[s])
    values this step1 3s
  13. result ← ['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']result
  14. for s in scores:

    3result = []4for s in scores:5    result.append(mapping[s])
  15. 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, use Series.replace(dict) instead.
  • Series.map also accepts a function: df['score'].map(str) converts each element to a string. This is the same elementwise API as Series.apply — each Python call runs per element.
  • map-lookup-column (ch05) uses .map for 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.