How to Perform a COUNTIF Function in Python?

28 Nov, 2021
In this article, we will discuss how to perform a COUNTIF function in Python.
We use this function to count the elements if the condition is satisfied. Notice that the word stands as COUNT + IF. That means we want to count the element if the condition that is provided is satisfied.
Approach
We will have a DataFrame with some columns.
We will use the function sum(). The sum() function will take an Iterable value. We will have a data frame with columns containing a list of elements. Then we will pass the condition to check whether the current element satisfies it or not.
sum() returns an integer value. So we will store the value and print it.
Syntax
The syntax of the sum() function is as follows.
sum(data-list condition)
Let us take an example where we have a list called myList and in the list, there are integer values. We want the number of items greater than equals 40. So we can use the sum function as follows,
sum(mylist >= 40)
For using two conditions, we can either use AND( & ) or OR( | ) for separating the two conditions.
sum((myList) >= 40 & (myList <= 90)) # AND
sum((myList) >= 40 | (myList <= 90)) # OR
First, let us create a DataFrame. Here we have two columns, which are views and likes. We will keep the length of each column the same.
Python3
# create a dictionarymy_data = {"views": [12, 13, 100, 80, 91],           "likes": [3, 8, 23, 17, 56]}  # convert to dataframemy_df = pd.DataFrame(my_data)
We will use the sum() function to check if, in the list of views column, the values are greater than 30. Then the sum function will count the rows that have corresponding views greater than 30.
Python3
import pandas as pd  # Datamy_data = {"views": [12, 13, 100, 80, 91],            "likes": [3, 8, 23, 17, 56]}my_df = pd.DataFrame(my_data)  # Printing the DataFrameprint(my_df.to_string())  # Printing the number of views greater# than 30print("View greater than 30: ",      sum(my_df.views > 30))
Output
The sum() function to check if, in the list of likes column, the values are greater than 20. Then the sum function will count the rows that have corresponding likes greater than 20.
Python3
import pandas as pd  # Datamy_data = {"views": [12, 13, 100, 80, 91],           "likes": [3, 8, 23, 17, 56]}my_df = pd.DataFrame(my_data)  # Printing the DataFrameprint(my_df.to_string())  # Printing the number of likes greater# than 20print("Likes greater than 20: ",       sum(my_df.likes > 20))
Output
For satisfying two or more conditions, wrap each condition in brackets( ) and then use single & sign to separate them. Here we have only two conditions, so we need only one &.
Python3
import pandas as pd  # Datamy_data = {"views": [12, 13, 100, 80, 91], "likes": [3, 8, 23, 17, 56]}my_df = pd.DataFrame(my_data)  # DataFrame  # Printing the DataFrameprint(my_df.to_string())  # Calculating the number of views greater than 30# as well as likes less than 20sum = sum((my_df.likes < 20) & (my_df.views > 30))print("Likes less than 20 and Views more than 30: ", sum)
Output
We will use a single | sign to separate the conditions. | is used as either the first condition OR second OR third and so on.
Python3
import pandas as pd  # Datamy_data = {"views": [12, 13, 100, 80, 91], "likes": [3, 8, 23, 17, 56]}my_df = pd.DataFrame(my_data)  # DataFrame  # Printing the DataFrameprint(my_df.to_string())  # Calculating the number of views greater than 30# or likes less than 20sum = sum((my_df.likes < 20) | (my_df.views > 30))print("Likes less than 20 or Views more than 30: ", sum)
Output
Picked
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.