Filter the expression based on one or more predicate expressions
Description
Elements where the filter does not evaluate to TRUE
are
discarded, including nulls. This is mostly useful in an aggregation
context. If you want to filter on a DataFrame level, use
DataFrame$filter()
or LazyFrame$filter()
.
Usage
<Expr>$filter(...)
Arguments
…
|
\<dynamic-dots \> Expression(s) that evaluate to a boolean
Series.
|
Value
A polars expression
Examples
#> shape: (3, 2)
#> ┌───────────┬─────┐
#> │ group_col ┆ b │
#> │ --- ┆ --- │
#> │ str ┆ f64 │
#> ╞═══════════╪═════╡
#> │ g1 ┆ 1.0 │
#> │ g1 ┆ 2.0 │
#> │ g2 ┆ 3.0 │
#> └───────────┴─────┘
df$group_by("group_col")$agg(
lt = pl$col("b")$filter(pl$col("b") < 2),
gte = pl$col("b")$filter(pl$col("b") >= 2)
)
#> shape: (2, 3)
#> ┌───────────┬───────────┬───────────┐
#> │ group_col ┆ lt ┆ gte │
#> │ --- ┆ --- ┆ --- │
#> │ str ┆ list[f64] ┆ list[f64] │
#> ╞═══════════╪═══════════╪═══════════╡
#> │ g1 ┆ [1.0] ┆ [2.0] │
#> │ g2 ┆ [] ┆ [3.0] │
#> └───────────┴───────────┴───────────┘