polars pivot
for dataframe
Pivot a DataFrame from wide to long format.
This command requires a plugin
The polars pivot
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 pivot {flags}
Flags
--on, -o {list<string>}
: column names for pivoting--index, -i {list<string>}
: column names for indexes--values, -v {list<string>}
: column names used as value columns--aggregate, -a {string}
: Aggregation to apply when pivoting. The following are supported: first, sum, min, max, mean, median, count, last--sort, -s
: Sort columns--streamable, -t
: Whether or not to use the polars streaming engine. Only valid for lazy dataframes
Input/output types:
input | output |
---|---|
any | any |
Examples
Perform a pivot in order to show individuals test score by subject
> [[name subject test_1 test_2]; [Cady maths 98 100] [Cady physics 99 100] [Karen maths 61 60] [Karen physics 58 60]] | polars into-df | polars pivot --on [subject] --index [name] --values [test_1]
╭───┬───────┬───────┬─────────╮
│ # │ name │ maths │ physics │
├───┼───────┼───────┼─────────┤
│ 0 │ Cady │ 98 │ 99 │
│ 1 │ Karen │ 61 │ 58 │
╰───┴───────┴───────┴─────────╯