How to Remove Element from a NumPy Array?

This tutorial will discuss about unique ways to remove element from a numpy array.

Remove An Element From NumPy Array by Value

Suppose we have a numpy array of numbers i.e.

numbers = np.array([22, 45, 34, 78, 45, 22])

And we want to remove number 45 from this array.

First of all we will compare the give element with the array using the != operator i.e.

# Create a boolean mask for the elements
# that are not equal to the element toRemove
mask = numbers != toRemove

It will return a boolean mask/array where each True value denotes that the corresponding value in original numpy array is not equal to the value that needs to be deleted. Whereas, each False value denotes the it is equal to the value that needs to be removed.

Then we will pass this boolean mask into the subscript operator of numpy array, and it will return a new array containing only those values from the numpy array, for which this boolean mask has value True.

# Use the mask to select only few elements
# from NumPy Array
numbers = numbers[mask]

Basically it will remove all the occurrences of given element from the numpy array.

In the below example, we will delete element with value 45 from the numpy array.

Let’s see the complete example,

import numpy as np

# Create a NumPy Array
numbers = np.array([22, 45, 34, 78, 45, 22])

# Element that need to be removed
toRemove = 45

# Create a boolean mask for the elements
# that are not equal to the element toRemove
mask = numbers != toRemove

# Use the mask to select only few elements
# from NumPy Array
numbers = numbers[mask]

# Print the NumPy array
print(numbers)

Output

[22 34 78 22]

Remove An Element From NumPy Array Using Index Position

Suppose we have a numpy array and we want to remove the element index position one from this array. Basically we want to delete the Nth element from this array.

For this we will call the delete() function of numpy module. In that we will pass the numpy array as first argument and index position as a second argument. It will delete the element at given index position, from the copy of numpy array and returns that. We will assign it to the same variable, and it will give us an effect that we have deleted it element at the given index position from the number array.

In the below example, we will delete the 2nd element from array i.e. element at index 1.

Let’s see the complete example,

import numpy as np

# Create a NumPy Array
numbers = np.array([22, 45, 34, 78, 45, 22])

# Index of the element to remove
indexToRemove = 1

# Use the `delete` function to remove the
# element at the specified index
numbers = np.delete(numbers, indexToRemove)

# Print the NumPy array
print(numbers)

Output

[22 34 78 45 22]

Summary

We learned how to delete an element from numpy array, either by value or index position in Python

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top