.count()
The .count()
method produces a new Series
or DataFrame
with counts of the values for each group in a in a GroupBy
object.
Syntax
groupbyobject.count()
Example
The following example produces a GroupBy
object from a DataFrame
and executes the .count()
method on it.
import pandas as pddf = pd.DataFrame({'Key' : ['A', 'A', 'A', 'B', 'B', 'C'],'Value' : [15., 23., 17., 5., 8., 12.]})print(df, end='\n\n')group = df.groupby(['Key'], as_index=False)print(group.count())
This example produces the following output:
Key Value0 A 15.01 A 23.02 A 17.03 B 5.04 B 8.05 C 12.0Key Value0 A 31 B 22 C 1