polars.DataFrame.partition_by#

DataFrame.partition_by(groups: Union[str, Sequence[str]], maintain_order: bool = False, *, as_dict: Literal[False] = False) list[DF][source]#
DataFrame.partition_by(groups: Union[str, Sequence[str]], maintain_order: bool = False, *, as_dict: Literal[True]) dict[Any, DF]
DataFrame.partition_by(groups: Union[str, Sequence[str]], maintain_order: bool, *, as_dict: bool) list[DF] | dict[Any, DF]

Split into multiple DataFrames partitioned by groups.

Parameters:
groups

Groups to partition by.

maintain_order

Keep predictable output order. This is slower as it requires an extra sort operation.

as_dict

If True, return the partitions in a dictionary keyed by the distinct group values instead of a list.

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": ["A", "A", "B", "B", "C"],
...         "N": [1, 2, 2, 4, 2],
...         "bar": ["k", "l", "m", "m", "l"],
...     }
... )
>>> df.partition_by(groups="foo", maintain_order=True)
[shape: (2, 3)
 ┌─────┬─────┬─────┐
 │ foo ┆ N   ┆ bar │
 │ --- ┆ --- ┆ --- │
 │ str ┆ i64 ┆ str │
 ╞═════╪═════╪═════╡
 │ A   ┆ 1   ┆ k   │
 ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
 │ A   ┆ 2   ┆ l   │
 └─────┴─────┴─────┘,
 shape: (2, 3)
 ┌─────┬─────┬─────┐
 │ foo ┆ N   ┆ bar │
 │ --- ┆ --- ┆ --- │
 │ str ┆ i64 ┆ str │
 ╞═════╪═════╪═════╡
 │ B   ┆ 2   ┆ m   │
 ├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
 │ B   ┆ 4   ┆ m   │
 └─────┴─────┴─────┘,
 shape: (1, 3)
 ┌─────┬─────┬─────┐
 │ foo ┆ N   ┆ bar │
 │ --- ┆ --- ┆ --- │
 │ str ┆ i64 ┆ str │
 ╞═════╪═════╪═════╡
 │ C   ┆ 2   ┆ l   │
 └─────┴─────┴─────┘]
>>> df.partition_by(groups="foo", maintain_order=True, as_dict=True)
{'A': shape: (2, 3)
┌─────┬─────┬─────┐
│ foo ┆ N   ┆ bar │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ A   ┆ 1   ┆ k   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ A   ┆ 2   ┆ l   │
└─────┴─────┴─────┘, 'B': shape: (2, 3)
┌─────┬─────┬─────┐
│ foo ┆ N   ┆ bar │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ B   ┆ 2   ┆ m   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ B   ┆ 4   ┆ m   │
└─────┴─────┴─────┘, 'C': shape: (1, 3)
┌─────┬─────┬─────┐
│ foo ┆ N   ┆ bar │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ C   ┆ 2   ┆ l   │
└─────┴─────┴─────┘}