Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,9 +1265,40 @@ def count(self):
@Appender(_doc_template)
def mean(self, *args, **kwargs):
"""
Compute mean of groups, excluding missing values
Compute mean of the target column for groups that are defined by the groupby columns.
Missing values are excluded in computing the mean.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line here for the Extended Summary

If there is one groupby column, the groups are the unique values in the specified groupby column.
If there are multiple groupby columns, the groups are the unique combinations of the specified groupby columns.

Returns
-------
pandas.core.series.Series
The average of the target column ('B' in the examples below)
grouped by the groupby columns ('A' and ['A', 'C'] in the examples below)

Example of groupby one column:the groups are stored as index in the result.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use underlines, rather use a blank line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this a sentence.
Groubpy by one column. The result index are the group labels.

---------------------------------------------------------------------------
>>> df = pd.DataFrame({'A': [1, 1, 2, 1, 2],
... 'B': [np.nan, 2, 3, 4, 5]}, columns=['A', 'B'])
>>> g = df.groupby('A')['B'].mean()
>>> g
A
1 3.0
2 4.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to put the Example at the end, see https://python-sprints.github.io/pandas/guide/pandas_docstring.html

Example of groupby multiple columns: the result index will be a MultiIndex.
---------------------------------------------------------------------------
>>> 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'])
>>> g = df.groupby(['A', 'C'])['B'].mean()
>>> g
A C
1 1 4.0
2 2.0
2 1 3.0
2 5.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to put the Example at the end, see https://python-sprints.github.io/pandas/guide/pandas_docstring.html

For multiple groupings, the result index will be a MultiIndex
"""
nv.validate_groupby_func('mean', args, kwargs, ['numeric_only'])
try:
Expand Down