polars.dataframe.groupby.GroupBy.__iter__#

GroupBy.__iter__() Self[source]#

Allows iteration over the groups of the groupby operation.

Each group is represented by a tuple of (name, data).

Examples

>>> df = pl.DataFrame({"foo": ["a", "a", "b"], "bar": [1, 2, 3]})
>>> for name, data in df.groupby("foo"):  
...     print(name)
...     print(data)
...
a
shape: (2, 2)
┌─────┬─────┐
│ foo ┆ bar │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═════╡
│ a   ┆ 1   │
│ a   ┆ 2   │
└─────┴─────┘
b
shape: (1, 2)
┌─────┬─────┐
│ foo ┆ bar │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═════╡
│ b   ┆ 3   │
└─────┴─────┘