polars unique

For [dataframe or lazyframe](/commands/categories/dataframe or lazyframe.md).

Returns unique values from a dataframe.

warning 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.

Command Type

plugin

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
polars_dataframe polars_dataframe
polars_lazyframe polars_lazyframe
polars_expression polars_expression
polars_selector polars_expression

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 │
╰───┴───┴───┴───╯

Returns unique values in a subset of lazyframe columns

> [[a]; [2] [1] [2]]
    | polars into-lazy
    | polars select (polars col a | polars unique)
    | polars collect
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ 1 │
│ 1 │ 2 │
╰───┴───╯

Returns unique values in a subset of lazyframe columns

> [[a]; [2] [1] [2]]
    | polars into-lazy
    | polars select (polars col a | polars unique --maintain-order)
    | polars collect
╭───┬───╮
│ # │ a │
├───┼───┤
│ 0 │ 2 │
│ 1 │ 1 │
╰───┴───╯

Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.