polars.Expr.value_counts#

Expr.value_counts(*, multithreaded: bool = False, sort: bool = False) Self[source]#

Count all unique values and create a struct mapping value to count.

Parameters:
multithreaded:

Better to turn this off in the aggregation context, as it can lead to contention.

sort:

Ensure the output is sorted from most values to least.

Returns:
Expr

Expression of data type Struct.

Examples

>>> df = pl.DataFrame(
...     {
...         "id": ["a", "b", "b", "c", "c", "c"],
...     }
... )
>>> df.select(
...     [
...         pl.col("id").value_counts(sort=True),
...     ]
... )
shape: (3, 1)
┌───────────┐
│ id        │
│ ---       │
│ struct[2] │
╞═══════════╡
│ {"c",3}   │
│ {"b",2}   │
│ {"a",1}   │
└───────────┘