polars.Expr.unique_counts
-
Return a count of the unique values in the order of appearance.
This method differs from
value_countsin that it does not return the values, only the counts and might be fasterExamples
>>> df = pl.DataFrame( ... { ... "id": ["a", "b", "b", "c", "c", "c"], ... } ... ) >>> df.select(pl.col("id").unique_counts()) shape: (3, 1) ┌─────┐ │ id │ │ --- │ │ u32 │ ╞═════╡ │ 1 │ │ 2 │ │ 3 │ └─────┘Note that
group_bycan be used to generate counts.>>> df.group_by("id", maintain_order=True).len().select("len") shape: (3, 1) ┌─────┐ │ len │ │ --- │ │ u32 │ ╞═════╡ │ 1 │ │ 2 │ │ 3 │ └─────┘To add counts as a new column
pl.len()can be used as a window function.>>> df.with_columns(pl.len().over("id")) shape: (6, 2) ┌─────┬─────┐ │ id ┆ len │ │ --- ┆ --- │ │ str ┆ u32 │ ╞═════╪═════╡ │ a ┆ 1 │ │ b ┆ 2 │ │ b ┆ 2 │ │ c ┆ 3 │ │ c ┆ 3 │ │ c ┆ 3 │ └─────┴─────┘
Expr.unique_counts() → Expr
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.unique_counts.html