Home » Pandas: How to Count Values in Column with Condition

Pandas: How to Count Values in Column with Condition

by Erma Khan

You can use the following methods to count the number of values in a pandas DataFrame column with a specific condition:

Method 1: Count Values in One Column with Condition

len(df[df['col1']=='value1'])

Method 2: Count Values in Multiple Columns with Conditions

len(df[(df['col1']=='value1') & (df['col2']=='value2')])

The following examples show how to use each method in practice with the following pandas DataFrame:

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

Example 1: Count Values in One Column with Condition

The following code shows how to count the number of values in the team column where the value is equal to ‘A’:

#count number of values in team column where value is equal to 'A'
len(df[df['team']=='A'])

4

We can see that there are 4 values in the team column where the value is equal to ‘A.’

Example 2: Count Values in Multiple Columns with Conditions

The following code shows how to count the number of rows in the DataFrame where the team column is equal to ‘B’ and the pos column is equal to ‘Gu’:

#count rows where team is 'B' and pos is 'Gu'
len(df[(df['team']=='B') & (df['pos']=='Gu')])

2

We can see that there are 2 rows in the DataFrame that meet both of these conditions.

We can use similar syntax to count the number of rows that meet any number of conditions we’d like.

For example, the following code shows how to count the number of rows that meet three conditions:

  • team is equal to ‘B’
  • pos is equal to ‘Gu’
  • points is greater than 12
#count rows where team is 'B' and pos is 'Gu' and points > 15
len(df[(df['team']=='B') & (df['pos']=='Gu') & (df['points']>12)])

1

We can see that only 1 row in the DataFrame meets all three of these conditions.

Additional Resources

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

Pandas: How to Find the Difference Between Two Rows
Pandas: How to Drop Rows that Contain a Specific String
Pandas: How to Drop Duplicate Rows in a DataFrame

Related Posts