16.2 C
London
Wednesday, June 4, 2025
HomePandas in PythonGeneral Functions in PythonPandas: How to Use Groupby and Count with Condition

Pandas: How to Use Groupby and Count with Condition

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to perform a groupby and count with condition in a pandas DataFrame:

df.groupby('var1')['var2'].apply(lambda x: (x=='val').sum()).reset_index(name='count')

This particular syntax groups the rows of the DataFrame based on var1 and then counts the number of rows where var2 is equal to ‘val.’

The following example shows how to use this syntax in practice.

Example: Groupby and Count with Condition in Pandas

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'pos': ['Gu', 'Fo', 'Fo', 'Fo', 'Gu', 'Gu', 'Fo', 'Fo'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28]})

#view DataFrame
print(df)

  team pos  points
0    A  Gu      18
1    A  Fo      22
2    A  Fo      19
3    A  Fo      14
4    B  Gu      14
5    B  Gu      11
6    B  Fo      20
7    B  Fo      28

The following code shows how to group the DataFrame by the team variable and count the number of rows where the pos variable is equal to ‘Gu’:

#groupby team and count number of 'pos' equal to 'Gu'
df_count = df.groupby('team')['pos'].apply(lambda x: (x=='Gu').sum()).reset_index(name='count')

#view results
print(df_count)

  team  count
0    A      1
1    B      2

From the output we can see:

  • Team A has 1 row where the pos column is equal to ‘Gu’
  • Team B has 2 rows where the pos column is equal to ‘Gu’

We can use similar syntax to perform a groupby and count with some numerical condition.

For example, the following code shows how to group by the team variable and count the number of rows where the points variable is greater than 15:

#groupby team and count number of 'points' greater than 15
df_count = df.groupby('team')['points'].apply(lambda x: (x>15).sum()).reset_index(name='count')

#view results
print(df_count)

  team  count
0    A      3
1    B      2

From the output we can see:

  • Team A has 3 rows where the points column is greater than 15
  • Team B has 2 rows where the points column is greater than 15 

You can use similar syntax to perform a groupby and count with any specific condition you’d like.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Count Unique Values Using Pandas GroupBy
How to Apply Function to Pandas Groupby
How to Create Bar Plot from Pandas GroupBy

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories