polars.dataframe.group_by.GroupBy.__iter__#

GroupBy.__iter__() Self[source]#

Allows iteration over the groups of the group by 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.group_by("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   │
└─────┴─────┘