polars unique
for [dataframe or lazyframe](/commands/categories/dataframe or lazyframe.md)
Returns unique values from a dataframe.
This command requires a plugin
The polars unique
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 unique {flags}
Flags
--subset, -s {any}
: Subset of column(s) to use to maintain rows (lazy df)--last, -l
: Keeps last unique value. Default keeps first value (lazy df)--maintain-order, -k
: Keep the same order as the original DataFrame (lazy df)
Input/output types:
input | output |
---|---|
any | any |
Examples
Returns unique values from a series
> [2 2 2 2 2] | polars into-df | polars unique
╭───┬───╮
│ # │ 0 │
├───┼───┤
│ 0 │ 2 │
╰───┴───╯
Returns unique values in a subset of lazyframe columns
> [[a b c]; [1 2 1] [2 2 2] [3 2 1]] | polars into-lazy | polars unique --subset [b c] | polars collect
╭───┬───┬───┬───╮
│ # │ a │ b │ c │
├───┼───┼───┼───┤
│ 0 │ 1 │ 2 │ 1 │
│ 1 │ 2 │ 2 │ 2 │
╰───┴───┴───┴───╯
Returns unique values in a subset of lazyframe columns
> [[a b c]; [1 2 1] [2 2 2] [3 2 1]]
| polars into-lazy
| polars unique --subset [b c] --last
| polars collect
╭───┬───┬───┬───╮
│ # │ a │ b │ c │
├───┼───┼───┼───┤
│ 0 │ 2 │ 2 │ 2 │
│ 1 │ 3 │ 2 │ 1 │
╰───┴───┴───┴───╯
Creates a is unique expression from a column
> col a | unique