polars group-by for lazyframe

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

Signature

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

Parameters

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

Input/output types:

inputoutput
anyany

Examples

Group by and perform an aggregation

> [[a b]; [1 2] [1 4] [2 6] [2 4]]
    | polars into-df
    | 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")
     ]
╭───┬───┬───────┬───────┬───────╮
 # │ a │ b_min │ b_max │ b_sum │
├───┼───┼───────┼───────┼───────┤
 0 1     2     4     6
 1 2     4     6    10
╰───┴───┴───────┴───────┴───────╯

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
╭───┬───┬───────┬───────┬───────╮
 # │ a │ b_min │ b_max │ b_sum │
├───┼───┼───────┼───────┼───────┤
 0 1     2     4     6
 1 2     4     6    10
╰───┴───┴───────┴───────┴───────╯