Spec-Zone.ru › Nushell

polars group-by for lazyframe

Creates a group-by object that can be used for other aggregations.

Signature

> polars group-by {flags} ...rest

Flags

  • --maintain-order, -m: Ensure that the order of the groups is consistent with the input data. This is slower than a default group by and cannot be run on the streaming engine.

Parameters

  • ...rest: Expression(s) that define the lazy group-by.

Input/output types:

input output
polars_dataframe polars_dataframe
polars_lazyframe polars_lazyframe

Examples

Group by and perform an aggregation

> [[a b]; [1 2] [1 4] [2 6] [2 4]]
    | polars into-lazy
    | polars group-by a
    | polars agg [
        (polars col b | polars min | polars as "b_min")
        (polars col b | polars max | polars as "b_max")
        (polars col b | polars sum | polars as "b_sum")
     ]
    | polars collect
    | polars sort-by a
╭───┬───┬───────┬───────┬───────╮
│ # │ a │ b_min │ b_max │ b_sum │
├───┼───┼───────┼───────┼───────┤
│ 0 │ 1 │     2 │     4 │     6 │
│ 1 │ 2 │     4 │     6 │    10 │
╰───┴───┴───────┴───────┴───────╯

Group by an expression and perform an aggregation

> [[a b]; [2025-04-01 1] [2025-04-02 2] [2025-04-03 3] [2025-04-04 4]]
    | polars into-lazy
    | polars group-by (polars col a | polars get-day | $in mod 2)
    | polars agg [
        (polars col b | polars min | polars as "b_min")
        (polars col b | polars max | polars as "b_max")
        (polars col b | polars sum | polars as "b_sum")
     ]
    | polars collect
    | polars sort-by a
╭───┬───┬───────┬───────┬───────╮
│ # │ a │ b_min │ b_max │ b_sum │
├───┼───┼───────┼───────┼───────┤
│ 0 │ 0 │     2 │     4 │     6 │
│ 1 │ 1 │     1 │     3 │     4 │
╰───┴───┴───────┴───────┴───────╯

Copyright © 2019–2026 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/polars_group-by.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API