.count()

Published Jun 11, 2022
Contribute to Docs

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 pd
df = 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 Value
0 A 15.0
1 A 23.0
2 A 17.0
3 B 5.0
4 B 8.0
5 C 12.0
Key Value
0 A 3
1 B 2
2 C 1

All contributors

Looking to contribute?

Learn Python:Pandas on Codecademy