Skip to Content

Numpy – Count Values Between a Given Range

In this tutorial, we will look at how to count the values in a Numpy array that are between a given range, let’s say from start to end (both inclusive) with the help of some examples

Steps to get the count of all the values between a range in Numpy

count values in a numpy array between a given range

In general, to find the count of values in a Numpy array that satisfy the given condition, you can –

  1. Use boolean indexing to filter the array for only the values that satisfy the condition.
  2. Calculate the length of the filtered array from step 1.

Thus, first, filter the Numpy array to contain only the values that lie between the specified range and then find its length to get the required count.

Let’s now look at a step-by-step example.

Step 1 – Create a Numpy array

First, we will create a Numpy array that we will be using throughout this tutorial.

import numpy as np

# create a numpy array
ar = np.array([1, 2, 3, 5, 8, 9])
# display the array
print(ar)

Output:

[1 2 3 5 8 9]

Here, we used the numpy.array() function to create a one-dimensional Numpy array containing some numbers.

Step 2 – Filter the array using a boolean expression

To get all the values from a Numpy array that lie in a given range, filter the array using boolean indexing.

First, we will specify our boolean expression, (ar >= start) & (ar <= end) and then use the boolean array resulting from this expression to filter our original array.

For example, let’s get all the values in the above array that lie between the range 2 to 6.

# values in array between the range [2, 6]
ar_filtered = ar[(ar >= 2) & (ar <= 6)]
print(ar_filtered)

Output:

[2 3 5]

We get all the values in the array ar that lie in the range [2, 6].

Step 3 – Get the length of the filtered array

To get the count of values that satisfy the given condition (whether it lies in a given range or not) find the length of the resulting filtered array from step 2 using the Python built-in len() function.

# length of the filtered array
print(len(ar_filtered))

Output:

3

We get the count of values in the array ar that lie between the range 2 to 6 as 3.

We can combine the code from the last two steps into a single line of code.

# count of values in array between the range [2, 6]
print(len(ar[(ar >= 2) & (ar <= 6)]))

Output:

3

We get the same result as above and we removed the extra variable ar_filtered.

In this tutorial, we looked at how to count the values in a Numpy array that lie in a given range. Note that in this method we’re not counting the unique elements that are in that range, rather we’re counting all the values in an array that are in the given range (which may include duplicates depending on the array).

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.