polars.dataframe.groupby.GroupBy.n_unique#

GroupBy.n_unique() DataFrame[source]#

Count the unique values per group.

Examples

>>> df = 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"],
...     }
... )
>>> df.groupby("d", maintain_order=True).n_unique()
shape: (2, 3)
┌────────┬─────┬─────┐
│ d      ┆ a   ┆ b   │
│ ---    ┆ --- ┆ --- │
│ str    ┆ u32 ┆ u32 │
╞════════╪═════╪═════╡
│ Apple  ┆ 2   ┆ 2   │
│ Banana ┆ 3   ┆ 3   │
└────────┴─────┴─────┘