Ciaren

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/BPass).
  • 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
studentgrade
AliceA
BobC
CarolB
DaveF
4 rows · 2 cols
Map values (mapping: A→Pass, B→Pass; default=Fail; new_column=result)
After
studentgraderesultnew
AliceAPass
BobCFail
CarolBPass
DaveFFail
4 rows · 3 cols

Configuration

Config keyTypeRequiredDescription
columnstringYesColumn whose values are mapped
mappingobjectYes{ "value": "becomes" }
new_columnstringNoWrite to a new column (empty = overwrite column)
defaultanyNoValue for anything not in the mapping
use_defaultboolNoWhen 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 default automatically enables it — you only need use_default: false explicitly if you want unmapped values to pass through unchanged despite having set a default.
  • Set new_column to 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.

See also