numpy.where() in Python: Introduction, Syntax & Examples

The numpy.where() function in Python’s NumPy library is used for element-wise conditional operations on arrays. It allows us to apply conditions to elements in an array and return elements from different arrays based on those conditions.

Using numpy.where() function we can find indices where a condition is True which will help to locate a particular element in an array. When we deal with the missing or NaN values in our data we can use numpy.where() to replace that missing value with the appropriate value. This function has lots of use cases.

In this article, we will explore the numpy.where() function and its syntax along with some examples.

Also Read: Python NumPy divide() Function (With Examples)

Syntax of numpy.where() Function

Let us get started with the basic components of the numpy.where() function as shown below.

Syntax:

numpy.where(condition, x, y)

Parameters:

  • condition: This is the condition to be checked for each element in the array. It can be a boolean array or a condition that evaluates to a boolean value.
  • x: The value to be used when the condition is True.
  • y: The value to be used when the condition is False.

Examples of numpy.where() Function

There are three ways through which we can use numpy.where() function:

  • Using numpy.where() function with a single condition
  • Using numpy.where() function with multiple conditions
  • Using numpy.where() function without any condition

1. Using numpy.where() Function with a Single Condition

We can apply a single condition with np.where() function by putting the condition in () and using >, <, etc.

Example 1:

import numpy as np
arr = np.array([1,2,3,5,7,8,11])
result = np.where(arr<6)
result

In the above code to use the where() function we first imported the NumPy library as np then by using np we created an array and stored that array in a variable arr. Then we used the np.where() function with the condition that array elements should be less than 6 and then stored it in a variable result. Lastly, we printed the variable result from which we got the indices of the elements 1,2,3, and 5 as they are less than 6.

Output:

Example 1: Python numpy.where() Function with a Single condition

Example 2:

import numpy as np
arr = np.arange(20)
result = np.where(arr>15,"A","B")
result

In the above code, we have used np.arange() function for creating an array called arr of 20 elements from 0 to 19. Then in np.where() function we have written a condition that elements that are stored in arr and greater than 15 will be replaced with A and others will be replaced with B.

Output:

Example 2: Python numpy.where() Function with a Single condition


2. Using numpy.where() Function with Multiple Conditions

We can apply multiple conditions with the numpy.where() function by putting the condition in () and using &.

Example 1:

import numpy as np
arr = np.arange(50)
result = np.where((arr >=0) & (arr <=30), "0-30" ,arr)
result

Here we have used np.arange() function for creating an array called arr of 50 elements from 0 to 49. Then in np.where() function we have written conditions that elements which are stored in arr and which are greater than and equal to 0 and at the same time the elements which are less than and equal to 30 should be replaced with 0-30 and others should be replaced with the elements from the arr itself.

Output:

Example 1: Python numpy.where() Function with Multiple conditions

Example 2:

import numpy as np
arr = np.array([1,2,4,5,6,7])
result = np.where((arr>2) & (arr<6),['A','A','A','A','A','A'],['B','B','B','B','B','B'])
result

Output:

Example 2: Python numpy.where() Function with Multiple conditions


3. Using numpy.where() Function without Any Condition

In this case, the numpy.where() function returns the array according to the boolean condition. True will pick an element from the x array which is [1,2,4] and False will pick an element from the y array which is [7,8,9].

Example:

import numpy as np
result = np.where([True,False,False],[1,2,4],[7,8,9])
result

In the above code, we have only passed the boolean array i.e. [True, False, False] not the condition in the np.where() function so np.where() function iterated over the Boolean array and for each True value it picked the value from list one i.e [1,2,4] and for each False value it picked the value from the second list i.e [7,8,9]. Let’s see the result.

Output:

Example: Python numpy.where() Function without any condition

Summary

Numpy.where() is a fundamental function for element-wise conditional operations on arrays. We can effortlessly find the indices of the elements where a condition is satisfied by using this function. In this tutorial, we have discussed three different ways to use the numpy.where() function with examples. After reading this, we hope you can use this function easily in Python.

Reference

https://stackoverflow.com/questions/5642457/how-does-python-numpy-where-work

Priyanshu Singh
Priyanshu Singh
Articles: 44