Map values
Map values — mapValues
Map column values to new values via a lookup (CASE-WHEN-style), with an optional default for unmapped values.
Use cases
- Recode grades to outcomes (
A/B→Pass). - Translate codes to labels, sending anything unmapped to a default.
What it does
Looks up each value in the mapping table and replaces it. With use_default: true,
anything not in the mapping gets the default value.
Before
| student | grade |
|---|---|
| Alice | A |
| Bob | C |
| Carol | B |
| Dave | F |
4 rows · 2 cols
Map values (mapping: A→Pass, B→Pass; default=Fail; new_column=result)
After
| student | grade | resultnew |
|---|---|---|
| Alice | A | Pass |
| Bob | C | Fail |
| Carol | B | Pass |
| Dave | F | Fail |
4 rows · 3 cols
Configuration
| Config key | Type | Required | Description |
|---|---|---|---|
column | string | Yes | Column whose values are mapped |
mapping | object | Yes | { "value": "becomes" } |
new_column | string | No | Write to a new column (empty = overwrite column) |
default | any | No | Value for anything not in the mapping |
use_default | bool | No | When true, unmapped values become default. If omitted, it's inferred: true when a default value is set, false otherwise |
Generated Python code
# mapping: {"A": "Pass", "B": "Pass"}, default "Fail"
df_2 = df_1.assign(result=lambda _d: _d['grade'].map({'A': 'Pass', 'B': 'Pass'}).where(_d['grade'].isin(['A', 'B']), 'Fail'))
Tips & common mistakes
- Setting a
defaultautomatically enables it — you only needuse_default: falseexplicitly if you want unmapped values to pass through unchanged despite having set adefault. - Set
new_columnto keep the original column alongside the recoded one. - For one-to-one substitutions (with optional regex) prefer Replace values; for if/else logic over several columns, use Conditional column.