Replacing & Mapping Values
Python · Data Cleaning
Theory
Several methods change values without loops. s.replace('Bombay', 'Mumbai') swaps exact values, and a dict replaces many at once; df.replace({'city': {'Bombay': 'Mumbai'}}) limits it to one column, and regex=True matches patterns. s.map(dict) looks every value up in a dict and returns the mapped value - values missing from the dict become NaN, which is a handy way to spot unmapped codes. map also accepts a function.
For conditional changes, s.where(cond, other) keeps values where the condition is True and replaces the rest, while s.mask(cond, other) does the opposite and replaces where True. s.clip(lower=0, upper=5000) caps values into a range, useful for taming extreme amounts. np.where(cond, a, b) and np.select([conds], [values], default) build new columns from conditions, the pandas way to write CASE WHEN.
Why we use it
Real data uses codes, old names and inconsistent labels ('Bombay' vs 'Mumbai', 'M'/'F', 'Y'/'N'). Replacing and mapping standardises them so grouping and joining work correctly.
When to use it
Use replace for fixing known bad values, map for translating codes through a lookup (with NaN showing what is unmapped), where/mask for conditional overrides and clip for capping. For lookups that need several columns, merge with a lookup table instead.
How it works
- Column with values to change
- Fixing a few known values? Yes: s.replace({old: new}). No: Translate or override.
- Translating codes via a lookup? Yes: s.map(dict); NaN = unmapped. No: Conditional change.
- Use where / mask / clip or np.select for conditions
- Standardised column
Syntax
s.replace(old, new) / s.replace({old: new})
s.map(dict_or_func)
s.where(cond, other); s.mask(cond, other)
s.clip(lower, upper)
np.select(conditions, choices, default)Example
df['city'] = df['city'].replace({'Bombay': 'Mumbai'})
df['zone'] = df['city'].map({'Jaipur': 'North', 'Pune': 'West'})Key points
- map() turns values missing from the dict into NaN; replace() leaves them as they are
- where keeps values where True; mask replaces values where True
- clip(lower, upper) caps outliers without dropping rows
- np.select is CASE WHEN ... THEN ... ELSE default END
- SQL equivalent: CASE WHEN city = 'Bombay' THEN 'Mumbai' ELSE city END