You could use partition by. This would yield a dictionary
where the groupby
keys map to the partitioned DataFrames
.
df = pl.DataFrame({
"groups": [1, 1, 2, 2, 2],
"values": pl.arange(0, 5, eager=True)
})
part_dfs = df.partition_by("groups", as_dict=True)
print(part_dfs)
{1: shape: (2, 2)
┌────────┬────────┐
│ groups ┆ values │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞════════╪════════╡
│ 1 ┆ 0 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 1 ┆ 1 │
└────────┴────────┘,
2: shape: (3, 2)
┌────────┬────────┐
│ groups ┆ values │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞════════╪════════╡
│ 2 ┆ 2 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2 ┆ 3 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2 ┆ 4 │
└────────┴────────┘}