polars horizontal for expression
Horizontal calculation across multiple columns.
This command requires a plugin
The polars horizontal command resides in the polars plugin. To use this command, you must install and register nu_plugin_polars. See the Plugins chapter in the book for more information.
Signature
> polars horizontal {flags} (type) ...rest
Flags
--nulls, -n: If set, null value in the input will lead to null output
Parameters
type: horizontal operation. Values of all, any, min, max, sum, and mean are accepted....rest: Expression(s) that define the lazy group-by
Input/output types:
| input | output |
|---|---|
| any | polars_expression |
Examples
Horizontal sum across two columns (ignore nulls by default)
> [[a b]; [1 2] [2 3] [3 4] [4 5] [5 null]]
| polars into-df
| polars select (polars horizontal sum a b)
| polars collect
╭───┬─────╮
│ # │ sum │
├───┼─────┤
│ 0 │ 3 │
│ 1 │ 5 │
│ 2 │ 7 │
│ 3 │ 9 │
│ 4 │ 5 │
╰───┴─────╯Horizontal sum across two columns while accounting for nulls
> [[a b]; [1 2] [2 3] [3 4] [4 5] [5 null]]
| polars into-df
| polars select (polars horizontal sum a b --nulls)
| polars collect
╭───┬─────╮
│ # │ sum │
├───┼─────┤
│ 0 │ 3 │
│ 1 │ 5 │
│ 2 │ 7 │
│ 3 │ 9 │
│ 4 │ │
╰───┴─────╯