pandas.core.groupby.DataFrameGroupBy.size — pandas 3.0.0.dev0+2103.g41968a550a documentation (original) (raw)

DataFrameGroupBy.size()[source]#

Compute group sizes.

Returns:

DataFrame or Series

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.groupby

Apply a function groupby to a Series.

DataFrame.groupby

Apply 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