polars.Expr.where#

Expr.where(predicate: Expr) Expr[source]#

Filter a single column.

Alias for filter().

Parameters:
predicate

Boolean expression.

Examples

>>> df = pl.DataFrame(
...     {
...         "group_col": ["g1", "g1", "g2"],
...         "b": [1, 2, 3],
...     }
... )
>>> (
...     df.groupby("group_col").agg(
...         [
...             pl.col("b").where(pl.col("b") < 2).sum().alias("lt"),
...             pl.col("b").where(pl.col("b") >= 2).sum().alias("gte"),
...         ]
...     )
... ).sort("group_col")
shape: (2, 3)
┌───────────┬──────┬─────┐
│ group_col ┆ lt   ┆ gte │
│ ---       ┆ ---  ┆ --- │
│ str       ┆ i64  ┆ i64 │
╞═══════════╪══════╪═════╡
│ g1        ┆ 1    ┆ 2   │
├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┤
│ g2        ┆ null ┆ 3   │
└───────────┴──────┴─────┘