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). The group names are tuples of the distinct group values that identify each group. If a single string was passed to by, the keys are a single value instead of a tuple.

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   │
└─────┴─────┘