polars.Expr.filter#
- Expr.filter(predicate: Expr) Self [source]#
Filter a single column.
Mostly useful in an aggregation context. If you want to filter on a DataFrame level, use LazyFrame.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").filter(pl.col("b") < 2).sum().alias("lt"), ... pl.col("b").filter(pl.col("b") >= 2).sum().alias("gte"), ... ] ... ).sort("group_col") shape: (2, 3) ┌───────────┬─────┬─────┐ │ group_col ┆ lt ┆ gte │ │ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ i64 │ ╞═══════════╪═════╪═════╡ │ g1 ┆ 1 ┆ 2 │ │ g2 ┆ 0 ┆ 3 │ └───────────┴─────┴─────┘