Compute group sizes.
Number of rows in each group as a Series if as_index is True or a DataFrame if as_index is False.
See also
Series.groupbyApply a function groupby to a Series.
DataFrame.groupbyApply a function groupby to each row or column of a DataFrame.
Examples
For SeriesGroupBy:
>>> lst = ['a', 'a', 'b']
>>> ser = pd.Series([1, 2, 3], index=lst)
>>> ser
a 1
a 2
b 3
dtype: int64
>>> ser.groupby(level=0).size()
a 2
b 1
dtype: int64
>>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]]
>>> df = pd.DataFrame(data, columns=["a", "b", "c"],
... index=["owl", "toucan", "eagle"])
>>> df
a b c
owl 1 2 3
toucan 1 5 6
eagle 7 8 9
>>> df.groupby("a").size()
a
1 2
7 1
dtype: int64
For Resampler:
>>> ser = pd.Series([1, 2, 3], index=pd.DatetimeIndex(
... ['2023-01-01', '2023-01-15', '2023-02-01']))
>>> ser
2023-01-01 1
2023-01-15 2
2023-02-01 3
dtype: int64
>>> ser.resample('MS').size()
2023-01-01 2
2023-02-01 1
Freq: MS, dtype: int64
© 2008–2011, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
© 2011–2025, Open source contributors
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/2.3.0/reference/api/pandas.core.resample.Resampler.size.html