NumPy

Updating NumPy array elements and Boolean Indexing

In this tutorial, you will learn how to change the values of NumPy array elements and to filter and conditionally update values using Boolean Indexing.


Let's create a new 2D array:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ])
Python

If you need to update a specific element in a NumPy array, you can use direct assignment.

Here, we index the element in the 2nd row and 3rd column and assign a new value:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) # assign new value a[1,2] = 9999 print(a)
Python
Output

An amazing feature of NumPy is the ability to update values conditionally using Boolean Indexing.

Boolean Indexing allows you to select elements from an array based on conditions.

To do so, you first have to create a Boolean array by applying a condition to a NumPy array.

For example, here the condition we apply to our array is that elements should be less than 5:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) # create boolean array bool_arr = a < 5 print(bool_arr)
Python
Output

As you can see, we get an array of Booleans that has the same shape as the original one.

You then use this Boolean array to index the original array. Only the elements where the Boolean array is True will be selected.

This way, you can filter elements from an array:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) # create boolean array bool_arr = a < 5 # index 'a' using boolean array print(a[bool_arr])
Python
Output

But, you can also assign new values to those elements where the condition is True

Let's update all values that are less than 5:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) # create boolean array bool_arr = a < 5 # update values a[bool_arr] = 9999 print(a)
Python
Output

Instead of doing it in 2 steps, we can index and apply the condition at the same time:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) # conditionally update 'a' a[a < 5] = 9999 print(a)
Python
Output

What will be the output?

import numpy as np a = np.array([ [10, 7, 3], [8, 13, 6] ]) print(a >= 7)
Python

What will be the output?

import numpy as np a = np.array([ [10, 7, 3], [8, 13, 6] ]) b = a[a >= 7] print(b)
Python

What will be the output?

import numpy as np a = np.array([ [10, 7, 3], [8, 13, 6] ]) a[a >= 7] = 0 print(a)
Python

Another way to update multiple elements in a NumPy array is by slicing.

For example, here we update the entire first row using slicing:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) # update first row a[0,:] = [100, 200, 300] print(a)
Python
Output

Or the first 2 rows and first 2 columns:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) # update first 2 rows and first 2 cols a[:2,:2] = [[9999, 9999],[9999, 9999]] print(a)
Python
Output

Ensure that the shape of the array you assign matches the shape of the selected slice.

Here, we attempt to assign a 1D array to a 2D slice. Let's see what happens:

import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6] ]) a[:2,:2] = [9999, 9999, 9999, 9999] print(a)
Python
Output

What will be the output?

import numpy as np a = np.array([ [10, 7, 3], [8, 13, 6] ]) a[1,1] = 9999 print(a)
Python