Skip to content

Filter a single column.

Source code

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

library(polars)

df = pl$DataFrame(
  group_col = c("g1", "g1", "g2"),
  b = c(1, 2, 3)
)
df
#> 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]     │
#> └───────────┴───────────┴───────────┘