Ciaren

Replace values

Replace values — replaceValues

Substitute values in a column.

Use cases

  • Standardize codes (NNorth, Y/NYes/No).
  • Clean stray characters with a regex pattern.

What it does

Replaces each exact match of to_replace in the column with value. Non-matching values pass through unchanged.

Before
order_idregion
1001N
1002S
1003N
1004E
4 rows · 2 cols
Replace values (column=region, to_replace=N, value=North)
After
order_idregion
1001North
1002S
1003North
1004E
4 rows · 2 cols

Configuration

Config keyTypeRequiredDescription
columnstringYesColumn to edit
to_replaceanyYesValue (or regex pattern) to find
valueanyYesReplacement value
regexboolNoTreat to_replace as a regex (default false)

Generated Python code

df_2 = df_1.assign(region=lambda _d: _d['region'].replace('N', 'North'))

Tips & common mistakes

  • Literal vs regex: with regex: false the whole value must match; with regex: true, to_replace is a pattern and value is the substitution.
  • To map many values to new ones with an optional default, prefer Map values.

See also