VOOZH about

URL: https://www.geeksforgeeks.org/python/pandas-groupby-count-occurrences-in-column/

⇱ Pandas GroupBy - Count occurrences in column - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Pandas GroupBy - Count occurrences in column

Last Updated : 23 Jul, 2025

Using the size() or count() method with pandas.DataFrame.groupby() will generate the count of a number of occurrences of data present in a particular column of the dataframe. However, this operation can also be performed using pandas.Series.value_counts() and, pandas.Index.value_counts().

Approach

  • Import module
  • Create or import data frame
  • Apply groupby
  • Use any of the two methods
  • Display result

Method 1: Using pandas.groupyby().size()

The basic approach to use this method is to assign the column names as parameters in the groupby() method and then using the size() with it. Below are various examples that depict how to count occurrences in a column for different datasets.

Example 1:

In this example, we separately count occurrences of all the columns present in a dataset.

Output:

👁 Image

Example 2:

In the below program, we count occurrences of all the columns combined from the same dataset as used in the previous program.

Output:

👁 Image

Example 3:

Here, we separate count occurrences and combined count occurrences of the categorical columns present in a CSV file.

Output:

👁 Image
👁 Image

Method 2: Using pandas.groupyby().count()

The basic approach to use this method is to assign the column names as parameters in the groupby() method and then using the count() with it. Below are various examples that depict how to count occurrences in a column for different datasets.

Example 1:

In this example, we separately count occurrences of all the columns present in a dataset.

Output:

👁 Image

Example 2:

In the below program, we count occurrences of all the columns combined from the same dataset as used in the previous program.

Output:

👁 Image

Example 3:

Here, we separate count occurrences and combined count occurrences of the categorical columns present in a CSV file.

Comment