polars.lazyframe.group_by.LazyGroupBy.n_unique#

LazyGroupBy.n_unique() LazyFrame[source]#

Count the unique values per group.

Examples

>>> ldf = pl.DataFrame(
...     {
...         "a": [1, 2, 1, 3, 4, 5],
...         "b": [0.5, 0.5, 0.5, 10, 13, 14],
...         "d": ["Apple", "Banana", "Apple", "Apple", "Banana", "Banana"],
...     }
... ).lazy()
>>> ldf.group_by("d", maintain_order=True).n_unique().collect()
shape: (2, 3)
┌────────┬─────┬─────┐
│ d      ┆ a   ┆ b   │
│ ---    ┆ --- ┆ --- │
│ str    ┆ u32 ┆ u32 │
╞════════╪═════╪═════╡
│ Apple  ┆ 2   ┆ 2   │
│ Banana ┆ 3   ┆ 3   │
└────────┴─────┴─────┘