pandas.core.groupby.DataFrameGroupBy.mean — pandas 3.0.0.dev0+2097.gcdc5b7418e documentation (original) (raw)

DataFrameGroupBy.mean(numeric_only=False, skipna=True, engine=None, engine_kwargs=None)[source]#

Compute mean of groups, excluding missing values.

Parameters:

numeric_onlybool, default False

Include only float, int, boolean columns.

Changed in version 2.0.0: numeric_only no longer accepts None and defaults to False.

skipnabool, default True

Exclude NA/null values. If an entire group is NA, the result will be NA.

Added in version 3.0.0.

enginestr, default None

Added in version 1.4.0.

engine_kwargsdict, default None

Added in version 1.4.0.

Returns:

pandas.Series or pandas.DataFrame

Mean of values within each group. Same object type as the caller.

See also

Series.groupby

Apply a function groupby to a Series.

DataFrame.groupby

Apply a function groupby to each row or column of a DataFrame.

Examples

df = pd.DataFrame( ... {"A": [1, 1, 2, 1, 2], "B": [np.nan, 2, 3, 4, 5], "C": [1, 2, 1, 1, 2]}, ... columns=["A", "B", "C"], ... )

Groupby one column and return the mean of the remaining columns in each group.

df.groupby("A").mean() B C A 1 3.0 1.333333 2 4.0 1.500000

Groupby two columns and return the mean of the remaining column.

df.groupby(["A", "B"]).mean() C A B 1 2.0 2.0 4.0 1.0 2 3.0 1.0 5.0 2.0

Groupby one column and return the mean of only particular column in the group.

df.groupby("A")["B"].mean() A 1 3.0 2 4.0 Name: B, dtype: float64