How to Perform a COUNTIF Function in Python

The COUNTIF function in Python is a statistical function that allows you to count the number of values in a given array that meet a certain condition. It is accomplished by using the NumPy library to compare each value in the array to the condition and then counting the number of matches. This function can be very useful for finding patterns in data or for summarizing data.


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

x