polars filter for lazyframe
Filter dataframe based in expression.
Signature
> polars filter {flags} (filter expression)
Parameters
-
filter expression: Expression that define the column selection.
Input/output types:
| input | output |
|---|---|
| polars_dataframe | polars_dataframe |
| polars_lazyframe | polars_lazyframe |
| polars_expression | polars_expression |
Examples
Filter dataframe using an expression
> [[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars filter ((polars col a) >= 4) ╭───┬───┬───╮ │ # │ a │ b │ ├───┼───┼───┤ │ 0 │ 6 │ 2 │ │ 1 │ 4 │ 2 │ ╰───┴───┴───╯
Filter dataframe for rows where dt is within the last 2 days of the maximum dt value
> [[dt val]; [2025-04-01 1] [2025-04-02 2] [2025-04-03 3] [2025-04-04 4]] | polars into-df | polars filter ((polars col dt) > ((polars col dt | polars max | $in - 2day))) ╭───┬────────────┬─────╮ │ # │ dt │ val │ ├───┼────────────┼─────┤ │ 0 │ a year ago │ 3 │ │ 1 │ a year ago │ 4 │ ╰───┴────────────┴─────╯
Filter a single column in a group-by context
> [[a b]; [foo 1] [foo 2] [foo 3] [bar 2] [bar 3] [bar 4]] | polars into-df
| polars group-by a --maintain-order
| polars agg {
lt: (polars col b | polars filter ((polars col b) < 2) | polars sum)
gte: (polars col b | polars filter ((polars col b) >= 3) | polars sum)
}
| polars collect
╭───┬─────┬────┬─────╮
│ # │ a │ lt │ gte │
├───┼─────┼────┼─────┤
│ 0 │ foo │ 1 │ 3 │
│ 1 │ bar │ 0 │ 7 │
╰───┴─────┴────┴─────╯
Copyright © 2019–2026 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/polars_filter.html