polars.internals.dataframe.groupby.GroupBy.mean#

GroupBy.mean() DataFrame[source]#

Reduce the groups to the mean values.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 2, 2, 3, 4, 5],
...         "b": [0.5, 0.5, 4, 10, 13, 14],
...         "c": [True, True, True, False, False, True],
...         "d": ["Apple", "Orange", "Apple", "Apple", "Banana", "Banana"],
...     }
... )
>>> df.groupby("d", maintain_order=True).mean()
shape: (3, 4)
┌────────┬─────┬──────────┬──────┐
│ d      ┆ a   ┆ b        ┆ c    │
│ ---    ┆ --- ┆ ---      ┆ ---  │
│ str    ┆ f64 ┆ f64      ┆ bool │
╞════════╪═════╪══════════╪══════╡
│ Apple  ┆ 2.0 ┆ 4.833333 ┆ null │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ Orange ┆ 2.0 ┆ 0.5      ┆ null │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ Banana ┆ 4.5 ┆ 13.5     ┆ null │
└────────┴─────┴──────────┴──────┘