polars.when¶
- polars.when(expr: polars.internals.expr.Expr) polars.internals.whenthen.When ¶
Start a when, then, otherwise expression.
Examples
Below we add a column with the value 1, where column “foo” > 2 and the value -1 where it isn’t.
>>> df = pl.DataFrame({"foo": [1, 3, 4], "bar": [3, 4, 0]}) >>> df.with_column(pl.when(pl.col("foo") > 2).then(pl.lit(1)).otherwise(pl.lit(-1))) shape: (3, 3) ┌─────┬─────┬─────────┐ │ foo ┆ bar ┆ literal │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i32 │ ╞═════╪═════╪═════════╡ │ 1 ┆ 3 ┆ -1 │ ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ │ 3 ┆ 4 ┆ 1 │ ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ │ 4 ┆ 0 ┆ 1 │ └─────┴─────┴─────────┘
Or with multiple when, thens chained:
>>> df.with_column( ... pl.when(pl.col("foo") > 2) ... .then(1) ... .when(pl.col("bar") > 2) ... .then(4) ... .otherwise(-1) ... ) shape: (3, 3) ┌─────┬─────┬─────────┐ │ foo ┆ bar ┆ literal │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i32 │ ╞═════╪═════╪═════════╡ │ 1 ┆ 3 ┆ 4 │ ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ │ 3 ┆ 4 ┆ 1 │ ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤ │ 4 ┆ 0 ┆ 1 │ └─────┴─────┴─────────┘