What is np.where in Python?

You can use the function np.where() to work with NumPy arrays in Python. Its vectorized operations make it more efficient than loop-based methods.

What does np.where do?

The Python function np.where() is a powerful method from the NumPy library and is used for selecting elements from an array. It identifies and extracts elements that meet a certain condition and then returns indices or values that correspond to that condition.

The function is used in a variety of fields, such as data processing, scientific calculations, machine learning and data analysis. In data manipulation, np.where() makes it possible to filter data and replace values in arrays.

What is the syntax of np.where in Python?

The np.where() function takes a NumPy-like array that consists of, for example, integers or Boolean values. Its syntax looks like this:

import numpy as np
np.where(condition[, x, y])
python
  • condition: This is the condition that will be applied to the array in order to determine which elements will be selected.
  • x and y (optional): If you specify x and y, the values from x will be returned when the condition is met. Otherwise, the values from y are returned. If you don’t specify x and y, np.where() will return the indices that meet the condition.

After execution, np.where() returns a new NumPy array. The new array is the result of filtering or selecting elements from the original array based on the condition.

Examples of how to use np.where

The np.where() method is a versatile tool for applying conditions to arrays and is very useful for data manipulation. Below we’ll look at some examples for how to use it.

Replace elements in a NumPy array

The function np.where() in NumPy makes it possible to replace elements in an array based on a condition. Before getting started, you can change Python lists into arrays using np.array().

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
python

Now that we have our array, we want to define a condition for identifying elements larger than 3. We can do this with a condition that is True for elements that are larger than 3 and false otherwise:

condition = arr > 3
python

Next we’ll use np.where() and enter this condition. We’ll also establish that values that meet the condition will be replaced with -1 and all other values will be replaced with 0.

new_arr = np.where(condition, -1, 0)
python

The result, new_arr, contains the array that has been modified based on the condition. In the output, values that are greater than 3 are replaced with -1. All other values are replaced with 0.

print(new_arr)
python

Use np.where with a single condition

If you use np.where() with just one condition and without any replacement values, it will return a tuple of indices that the condition is true for.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
condition = arr > 2
result = np.where(condition)
print(result)
# Output: (array([2, 3, 4]),)
python

In this example, arr is a NumPy array that contains values from 1 to 5. condition = arr > 2 creates a Boolean mask that identifies elements in arr that are larger than 2. np.where(condition) is executed without replacement values and as a result, returns a tuple of indices that meet the condition arr > 2. The output is an array of indices in which the elements are larger than 2.

Broadcast with np.where

In NumPy, broadcasting makes it possible to perform operations with different shaped arrays, provided certain conditions are met. When arrays have different shapes, NumPy will try to enlarge them so they’re compatible.

Say we have a NumPy array arr with the form (3, 3):

import numpy as np
arr = np.array([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
python

And we have another NumPy array row with the form (3,):

row = np.array([10, 11, 12])
python

The forms of the two arrays don’t quite match, but broadcasting can be used to enable operations between them. If, say, we wanted to add the values from each row of arr with the values in row, broadcasting would make it possible.

result = arr + row
print(result)
# Output: [[11 13 15]
    [14 16 18]
    [17 19 21]]
python

In the example above, the one-dimensional array row was enlarged into a (3, 3) matrix to match the shape of arr. Then the elements of arr and row were added together element by element.

Web Hosting
Fast, scalable hosting for any website
  • 99.9% uptime
  • PHP 8.3 with JIT compiler
  • SSL, DDoS protection, and backups
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top