pandas.core.groupby.SeriesGroupBy.expanding#

SeriesGroupBy.expanding(min_periods=1, method='single')[source] #

Return an expanding grouper, providing expanding functionality per group.

Parameters:
min_periodsint, default 1

Minimum number of observations in window required to have a value; otherwise, result is np.nan.

methodstr {‘single’, ‘table’}, default ‘single’

Execute the expanding operation per single column or row ('single') or over the entire object ('table').

This argument is only implemented when specifying engine='numba' in the method call.

Returns:
pandas.api.typing.ExpandingGroupby

An object that supports expanding transformations over each group.

See also

Series.expanding

Expanding transformations for Series.

DataFrame.expanding

Expanding transformations for DataFrames.

Series.groupby

Apply a function groupby to a Series.

DataFrame.groupby

Apply a function groupby.

Examples

>>> df = pd.DataFrame(
...  {
...  "Class": ["A", "A", "A", "B", "B", "B"],
...  "Value": [10, 20, 30, 40, 50, 60],
...  }
... )
>>> df
Class Value
0 A 10
1 A 20
2 A 30
3 B 40
4 B 50
5 B 60
>>> df.groupby("Class").expanding().mean()
 Value
Class
A 0 10.0
 1 15.0
 2 20.0
B 3 40.0
 4 45.0
 5 50.0

This Page