Home »
Python »
Python Programs
How to remove specific elements in a numpy array?
In this tutorial, we will learn how to remove specific elements from a NumPy array?
By Pranit Sharma Last updated : May 24, 2023
NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.
Problem Statement
Suppose we are given a NumPy array that contains integer values from 1 to 100 and we need to remove all the multiples of 10 from this NumPy array.
Removing specific elements in a numpy array
To remove specific elements from a NumPy array, you can simply use the numpy.delete() method, which returns a new array with the sub-arrays along an axis deleted. In other words, it returns a copy of the array with the elements specified by the object removed. It is also important to note that deletion does not occur in place. The parameter that it takes is the array and a slice of indexes, int values, or an array of ints.
Python program to remove specific elements in a NumPy array
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([i for i in range(100)])
# Display original array
print("Orignal array:\n",arr,"\n")
# Defining some elements
multiples = [i for i in range(100) if i%10==0]
# Deleting these elements
res = np.delete(arr,multiples)
# Display result
print("Result:\n",res)
Output
Orignal array:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
96 97 98 99]
Result:
[ 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26
27 28 29 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 51 52 53
54 55 56 57 58 59 61 62 63 64 65 66 67 68 69 71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89 91 92 93 94 95 96 97 98 99]
Remove specific elements based on the given indices
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([10, 20, 30, 40, 50, 60, 70])
# Display original array
print("Orignal array:\n",arr,"\n")
# Defining the indices of the elements
# to delete
multiples = [0, 2, 3]
# Deleting these elements
res = np.delete(arr,multiples)
# Display result
print("Result:\n",res)
Output
Orignal array:
[10 20 30 40 50 60 70]
Result:
[20 50 60 70]
Remove specific elements based on the given values
To remove the given set of values from a NumPy array, you can use numpy.setdiff1d() method by passing the array and set of values to be deleted.
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([10, 20, 30, 40, 50, 60, 70])
# Display original array
print("Orignal array:\n",arr,"\n")
# Remove the elements based on the given values
# Here, we're removing 10, 40, and 70
res = np.setdiff1d(arr, [10, 40, 70])
# Display result
print("Result:\n",res)
Output
Orignal array:
[10 20 30 40 50 60 70]
Result:
[20 30 50 60]
Remove specific elements equal to the specific value (remove the multiple same values)
To remove specific elements equal to the specific value, you can use numpy.delete() method by passing the array and condition within the numpy.where() method.
# Import numpy
import numpy as np
# Creating a numpy array
arr = np.array([10, 20, 10, 40, 10, 60, 10])
# Display original array
print("Orignal array:\n",arr,"\n")
# Remove specific elements equal to the
# specific value (remove the multiple same values)
res = np.delete(arr, np.where(arr == 10))
# Display result
print("Result:\n",res)
Output
Orignal array:
[10 20 10 40 10 60 10]
Result:
[20 40 60]
Python NumPy Programs »