polars.lazyframe.group_by.LazyGroupBy.len#

LazyGroupBy.len(name: str | None = None) LazyFrame[source]#

Return the number of rows in each group.

Parameters:
name

Assign a name to the resulting column; if unset, defaults to “len”.

Examples

>>> lf = pl.LazyFrame({"a": ["Apple", "Apple", "Orange"], "b": [1, None, 2]})
>>> lf.group_by("a").len().collect()  
shape: (2, 2)
┌────────┬─────┐
│ a      ┆ len │
│ ---    ┆ --- │
│ str    ┆ u32 │
╞════════╪═════╡
│ Apple  ┆ 2   │
│ Orange ┆ 1   │
└────────┴─────┘
>>> lf.group_by("a").len(name="n").collect()  
shape: (2, 2)
┌────────┬─────┐
│ a      ┆ n   │
│ ---    ┆ --- │
│ str    ┆ u32 │
╞════════╪═════╡
│ Apple  ┆ 2   │
│ Orange ┆ 1   │
└────────┴─────┘