polars unpivot
for dataframe
Unpivot a DataFrame from wide to long format.
This command requires a plugin
The polars unpivot
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 unpivot {flags}
Flags
--index, -i {table}
: column names for unpivoting--on, -o {table}
: column names used as value columns--variable-name, -r {string}
: optional name for variable column--value-name, -l {string}
: optional name for value column--streamable, -t
: Whether or not to use the polars streaming engine. Only valid for lazy dataframes
Input/output types:
input | output |
---|---|
any | any |
Examples
unpivot on an eager dataframe
> [[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | polars into-df | polars unpivot -i [b c] -o [a d]
╭───┬───┬───┬──────────┬───────╮
│ # │ b │ c │ variable │ value │
├───┼───┼───┼──────────┼───────┤
│ 0 │ 1 │ 4 │ a │ x │
│ 1 │ 2 │ 5 │ a │ y │
│ 2 │ 3 │ 6 │ a │ z │
│ 3 │ 1 │ 4 │ d │ a │
│ 4 │ 2 │ 5 │ d │ b │
│ 5 │ 3 │ 6 │ d │ c │
╰───┴───┴───┴──────────┴───────╯
unpivot on a lazy dataframe
> [[a b c d]; [x 1 4 a] [y 2 5 b] [z 3 6 c]] | polars into-lazy | polars unpivot -i [b c] -o [a d] | polars collect
╭───┬───┬───┬──────────┬───────╮
│ # │ b │ c │ variable │ value │
├───┼───┼───┼──────────┼───────┤
│ 0 │ 1 │ 4 │ a │ x │
│ 1 │ 2 │ 5 │ a │ y │
│ 2 │ 3 │ 6 │ a │ z │
│ 3 │ 1 │ 4 │ d │ a │
│ 4 │ 2 │ 5 │ d │ b │
│ 5 │ 3 │ 6 │ d │ c │
╰───┴───┴───┴──────────┴───────╯