daft.Expression.list.value_counts

daft.Expression.list.value_counts#

Expression.list.value_counts() Expression[source]#

Counts the occurrences of each unique value in the list.

Returns:

A Map<X, UInt64> expression where the keys are unique elements from the

original list of type X, and the values are UInt64 counts representing the number of times each element appears in the list.

Return type:

Expression

Note

This function does not work for nested types. For example, it will not produce a map with lists as keys.

Example

>>> import daft
>>> df = daft.from_pydict({"letters": [["a", "b", "a"], ["b", "c", "b", "c"]]})
>>> df.with_column("value_counts", df["letters"].list.value_counts()).collect()
╭──────────────┬───────────────────╮
│ letters      ┆ value_counts      │
│ ---          ┆ ---               │
│ List[Utf8]   ┆ Map[Utf8: UInt64] │
╞══════════════╪═══════════════════╡
│ [a, b, a]    ┆ [{key: a,         │
│              ┆ value: 2,         │
│              ┆ }, {key: …        │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [b, c, b, c] ┆ [{key: b,         │
│              ┆ value: 2,         │
│              ┆ }, {key: …        │
╰──────────────┴───────────────────╯

(Showing first 2 of 2 rows)