polars.DataFrame.mean#

DataFrame.mean(*, axis: Literal[0] = 0, null_strategy: NullStrategy = 'ignore') DF[source]#
DataFrame.mean(*, axis: Literal[1], null_strategy: NullStrategy = 'ignore') Series
DataFrame.mean(*, axis: int = 0, null_strategy: NullStrategy = 'ignore') Union[DF, Series]

Aggregate the columns of this DataFrame to their mean value.

Parameters:
axis

Either 0 or 1.

null_strategy{‘ignore’, ‘propagate’}

This argument is only used if axis == 1.

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...         "ham": ["a", "b", "c"],
...     }
... )
>>> df.mean()
shape: (1, 3)
┌─────┬─────┬──────┐
│ foo ┆ bar ┆ ham  │
│ --- ┆ --- ┆ ---  │
│ f64 ┆ f64 ┆ str  │
╞═════╪═════╪══════╡
│ 2.0 ┆ 7.0 ┆ null │
└─────┴─────┴──────┘

Note: a PanicException is raised with axis = 1 and a string column.

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...     }
... )
>>> df.mean(axis=1)
shape: (3,)
Series: 'foo' [f64]
[
        3.5
        4.5
        5.5
]

Note: the mean of booleans evaluates to null.

>>> df = pl.DataFrame(
...     {
...         "a": [True, True, False],
...         "b": [True, True, True],
...     }
... )
>>> df.mean()
shape: (1, 2)
┌──────┬──────┐
│ a    ┆ b    │
│ ---  ┆ ---  │
│ bool ┆ bool │
╞══════╪══════╡
│ null ┆ null │
└──────┴──────┘

Instead, cast to numeric type:

>>> df.select(pl.all().cast(pl.UInt8)).mean()
shape: (1, 2)
┌──────────┬─────┐
│ a        ┆ b   │
│ ---      ┆ --- │
│ f64      ┆ f64 │
╞══════════╪═════╡
│ 0.666667 ┆ 1.0 │
└──────────┴─────┘