Filter a single column.
Description
Mostly useful in an aggregation context. If you want to filter on a
DataFrame level, use DataFrame$filter()
(or
LazyFrame$filter()
).
Usage
<Expr>$filter(predicate)
Arguments
predicate
|
An Expr or something coercible to an Expr. Must return a boolean. |
Value
Expr
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] │
#> └───────────┴───────────┴───────────┘