How to remove multiple elements from a NumPy array?

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

Remove Multiple Values from NumPy Array by Value

Suppose we have an array which contains the city names i.e.

cityNames = np.array(['NewYork', 'Paris', 'London', 'Tokyo', 'Dubai'])

We want to delete 2 city names i.e. ‘Paris’ and ‘London’, from this array.

To do this we will create a boolean array where each False value denotes the value that needs to be deleted from the array. First of all we will create a array where all values are True and then we will iterate over the values to be deleted and mark them False in boolean array.

So, we will get a boolean array where each True value represents that this value should not be deleted and all the values that need to be deleted are marked as False. Then we will pass this boolean array into the subscript operator of the array and it will delete it will return us a new array after removing the values for which boolean mask had False.

So, this wway we can delete multiple elements from NumPy Array.

Let’s see the complete example,

import numpy as np

# Create a NumPy Array
cityNames = np.array(['NewYork', 'Paris', 'London', 'Tokyo', 'Dubai'])

# Elements to remove
toRemove = ['Paris', 'London']

# Create boolean mask of True only
mask = np.ones(len(cityNames), dtype=bool)

# Update boolean mask for the elements
# that are not equal to the elements to remove
for element in toRemove:
    mask &= cityNames != element

# Use the mask to update the NumPy Array with
# the specified elements removed
cityNames = cityNames[mask]

# Print the NumPy Array
print(cityNames)

Output

['NewYork' 'Tokyo' 'Dubai']

Remove Multiple Values from NumPy Array by Index Positions

to remove multiple elements from NumPy array based on index position we can use the delete() function of the numpy module.

Duppose we have a NumPy Array, which contains the city names and another array which contains certain index positions.

Now, elements at these index positions need to be deleted from the numpy array. For that, we will call the delete() function of numpy module and we will pass the numpy array as first argument and as the second argument we will pass the list of index positions.

It will return a copy of numpy array after deleting the elements at the specified index positions.

Let’s see the complete example,

import numpy as np

# Create a NumPy Array
cityNames = np.array(['NewYork', 'Paris', 'London', 'Tokyo', 'Dubai'])

# Define the indices of the elements to remove
indicesToRemove = [1, 2]

# Use the `delete` function to remove the elements at the specified indices
cityNames = np.delete(cityNames, indicesToRemove)

# Print the NumPy Array
print(cityNames)

Output

['NewYork' 'Tokyo' 'Dubai']

Summary

We learned how to remove multiple elements from NumPy Array 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