map

ColExpr.map(
mapping: dict[tuple | ColExpr, ColExpr],
*,
default: ColExpr | None = None,
) CaseExpr[source]

Replaces given values by other expressions.

Parameters:
  • mapping – A dictionary of expressions / tuples of expressions to expressions. The input is compared against key of the dictionary, and if it matches, the corresponding value of the key is inserted. If the key is a tuple, the input is compared against each element of the tuple and required to equal at least one of them.

  • default – The value to insert if the input matches none of the keys of mapping.

Note

If there are multiple columns in the key which have the same value at some row, any of the corresponding values may be inserted (i.e. ensuring uniqueness of the keys is your responsibility).

Example

>>> t = pdt.Table(
...     {
...         "a": [4, 3, -35, 24, 105],
...     }
... )
>>> t >> mutate(map=t.a.map({4:True,3:True,-35:False,105:False}, default=pdt.lit(None))) >> show()
Table without name (backend: polars)
shape: (5, 2)
┌─────┬───────┐
│ a   ┆ map   │
│ --- ┆ ---   │
│ i64 ┆ bool  │
╞═════╪═══════╡
│ 4   ┆ true  │
│ 3   ┆ true  │
│ -35 ┆ false │
│ 24  ┆ null  │
│ 105 ┆ false │
└─────┴───────┘