Replace specific values in a column via a mapping, leaving all other values unchanged. By hand, mapping.get(v, v) returns the mapped value or the original. With pandas, Series.replace(dict) does the same in one call.

By hand

Loop over values, using mapping.get(v, v) to replace mapped values and return the original v for everything else. The unmapped values (2 and 4) pass through unchanged.

naive.py
Replay: real traced execution (multi-file project)
values = [1, 2, 3, 2, 4]
mapping = {1: 10, 3: 30}
result = []
for v in values:
    result.append(mapping.get(v, v))
print('RESULT:', result)
  1. values ← [1, 2, 3, 2, 4]

    1values = [1, 2, 3, 2, 4]2mapping = {1: 10, 3: 30}
    values this step[1, 2, 3, 2, 4]values
  2. mapping ← {1: 10, 3: 30}

    1values = [1, 2, 3, 2, 4]2mapping = {1: 10, 3: 30}3result = []
    values this step{1: 10, 3: 30}mapping
  3. result ← []

    2mapping = {1: 10, 3: 30}3result = []4for v in values:
    values this step[]result
  4. v ← 1

    3result = []4for v in values:5    result.append(mapping.get(v, v))
    values this step1v
  5. result ← [10]

    4for v in values:5    result.append(mapping.get(v, v))6print('RESULT:', result)
    values this step[] [10]result
  6. v ← 2

    3result = []4for v in values:5    result.append(mapping.get(v, v))
    values this step1 2v
  7. result ← [10, 2]

    4for v in values:5    result.append(mapping.get(v, v))6print('RESULT:', result)
    values this step[10] [10, 2]result
  8. v ← 3

    3result = []4for v in values:5    result.append(mapping.get(v, v))
    values this step2 3v
  9. result ← [10, 2, 30]

    4for v in values:5    result.append(mapping.get(v, v))6print('RESULT:', result)
    values this step[10, 2] [10, 2, 30]result
  10. v ← 2

    3result = []4for v in values:5    result.append(mapping.get(v, v))
    values this step3 2v
  11. result ← [10, 2, 30, 2]

    4for v in values:5    result.append(mapping.get(v, v))6print('RESULT:', result)
    values this step[10, 2, 30] [10, 2, 30, 2]result
  12. v ← 4

    3result = []4for v in values:5    result.append(mapping.get(v, v))
    values this step2 4v
  13. result ← [10, 2, 30, 2, 4]

    4for v in values:5    result.append(mapping.get(v, v))6print('RESULT:', result)
    values this step[10, 2, 30, 2] [10, 2, 30, 2, 4]result
  14. for v in values:

    3result = []4for v in values:5    result.append(mapping.get(v, v))
  15. stdout ← RESULT: [10, 2, 30, 2, 4]

    5    result.append(mapping.get(v, v))6print('RESULT:', result)
    values this stepRESULT: [10, 2, 30, 2, 4]stdout

With pandas

df['x'].replace(mapping) replaces 1→10 and 3→30, leaving 2 and 4 intact. The dtype stays int64 because all replacements and originals are integers.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

values = [1, 2, 3, 2, 4]
mapping = {1: 10, 3: 30}
df = pd.DataFrame({'x': values})
result = df['x'].replace(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: [10, 2, 30, 2, 4]
dtype: int64
RESULT: [10, 2, 30, 2, 4]

Implementation notes

  • Key contrast with .map: Series.replace(dict) leaves unmapped values unchanged; Series.map(dict) yields NaN for any key not in the mapping. Use .replace when you want partial substitution.
  • replace also accepts lists: df['x'].replace([1, 3], [10, 30]) is equivalent to the dict form above.
  • Cross-reference: map-values (this chapter) for the NaN-for-unmapped alternative using Series.map.