COUNTIF

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

How to Perform a COUNTIF Function in Python?

In this article, we will discuss how to perform a COUNTIF function in Python.

Similar Reads

COUNTIF

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....

Method 1: Using a single column

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....

Method 2: Using multiple columns

...

Contact Us