How to Perform a COUNTIF Function in Python


Often you may be interested in only counting the number of rows in a pandas DataFrame that meet some criteria.

Fortunately this is easy to do using the following basic syntax:

sum(df.column_name == some_value)

The following examples show how to use this syntax in practice on the following data frame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'x': [3, 4, 5, 6, 7, 8, 9, 10, 10, 12, 13],
                   'y': [3, 4, 5, 7, 9, 13, 15, 19, 23, 24, 29]})

#view head of DataFrame
df.head()

x	y
0	3	3
1	4	4
2	5	5
3	6	7
4	7	9

Example 1: Count Rows Equal to Some Value

The following code shows how to count the number of rows where the x variable is equal to 10:

sum(df.x == 10)

2

The following code shows how to count the number of rows where the x variable is equal to 10 or the y variable is equal to 5:

sum((df.x == 10) | (df.y == 5))

3

The following code shows how to count the number of rows where the x variable is not equal to 10:

sum(df.x != 10)

9

Example 2: Count Rows Greater or Equal to Some Value

The following code shows how to count the number of rows where x is greater than 10:

sum(df.x > 10) 

2

The following code shows how to count the number of rows where x is less than or equal to 7:

sum(df.x <= 7)
 
5

Example 3: Count Rows Between Two Values

The following code shows how to count the number of rows where x is between 10 and 20:

sum((df.x >= 5) & (df.x <= 10))

7

Additional Resources

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

Leave a Reply

Your email address will not be published.