How to remove specific elements from a NumPy array ?
Last Updated :
12 Sep, 2022
In this article, we will discuss how to remove specific elements from the NumPy Array.
Remove specific elements from a NumPy 1D array
Deleting element from NumPy array using np.delete()
The delete(array_name ) method will be used to do the same. Where array_name is the name of the array to be deleted and index-value is the index of the element to be deleted. For example, if we have an array with 5 elements, The indexing starts from 0 to n-1. If we want to delete 2, then 2 element index is 1. So, we can specify If we want to delete multiple elements i.e. 1,2,3,4,5 at a time, you can specify all index elements in a list.
Remove a Specific element in a 1D array
Program to create an array with 5 elements and delete the 1st element.
Python3
# import numpy as np
import numpy as np
# create an array with 5 elements
a = np.array([1, 2, 3, 4, 5])
# display a
print(a)
# delete 1 st element
print("remaining elements after deleting 1st element ",
np.delete(a, 0))
Output:
[1 2 3 4 5]
remaining elements after deleting 1st element [2 3 4 5]
Remove Multiple elements in a 1D array
Program to create an array with 5 elements and delete the 1st and last element.
Python3
# import numpy as np
import numpy as np
# create an array with 5
# elements
a = np.array([1, 2, 3, 4, 5])
# display a
print(a)
# delete 1st element and 5th element
print("remaining elements after deleting 1st and last element ",
np.delete(a, [0, 4]))
Output:
[1 2 3 4 5]
remaining elements after deleting 1st and last element [2 3 4]
Remove a Specific NumPy Array Element by Value in a 1D array
Removing 8 values from an array.
Python3
import numpy as np
arr_1D = np.array([1 ,2, 3, 4, 5, 6, 7, 8])
arr_1D = np.delete(arr_1D, np.where(arr_1D == 8))
print(arr_1D)
Output:
[1 2 3 4 5 6 7]
Removing multiple elements from a NumPy using an index
Pass an array containing the indexes of all the elements except the index of the element to be deleted, This will delete the element from the array.
Python3
import numpy as np
# numpy array
arr = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1])
# index array with index of all the elements, except index = 5.
# so element at 5th index will be deleted.
indexArray = [0, 1, 2, 3, 4, 6, 7, 8]
# passing indexarray to the array as index
arr = arr[indexArray]
print(arr)
Output:
[9 8 7 6 5 3 2 1]
Remove specific elements from a NumPy 2D array
Remove a column in a 2D array
Deleting 1st column.
Python3
import numpy as np
arr = np.array([[1 ,2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
print(np.delete(arr, 1, axis=1))
Output:
[[ 1 3 4]
[ 5 7 8]
[ 9 11 12]]
Remove a row in a 2D array
Deleting 1st row.
Python3
import numpy as np
arr = np.array([[1 ,2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
print(np.delete(arr, 1, axis=0))
Output:
[[ 1 2 3 4]
[ 9 10 11 12]]
Remove Multiple elements in a 2D array
Removing multiple elements from a 2D array. Here, we are removing 1st and last column from an array.
Python3
arr = np.delete(arr, [0,3], axis=1)
print(arr)
Output:
[[ 2 3]
[ 6 7]
[10 11]]
Similar Reads
How to Remove Duplicate Elements from NumPy Array In this article, we will see how to remove duplicate elements from NumPy Array. Here we will learn how to Remove Duplicate Elements from a 1-D NumPy Array and 2-D NumPy Array.Input1: [1 2 3 4 5 1 2 3 1 2 9] Output1: [1 2 3 4 5 9] Explanation: In this example, we have removed duplicate elements from
7 min read
How to delete last N rows from Numpy array? In this article, we will discuss how to delete the last N rows from the NumPy array. Method 1: Using Slice Operator Slicing is an indexing operation that is used to iterate over an array. Â Syntax: array_name[start:stop] where start is the start is the index and stop is the last index. We can also do
4 min read
NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4
2 min read
How to remove rows from a Numpy array based on multiple conditions ? In this article, we will learn how to remove rows from a NumPy array based on multiple conditions. For doing our task, we will need some inbuilt methods provided by the NumPy module which are as follows: np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on g
3 min read
How to remove NaN values from a given NumPy array? In this article, we are going to learn how to remove Nan values from a given array. Nan values are those values that do not have a specific value associated with them or they are different from the type of values that are to be used in the declared array. There are basically three approaches with sl
3 min read
Select an element or sub array by index from a Numpy Array The elements of a NumPy array are indexed just like normal arrays. The index of the first element will be 0 and the last element will be indexed n-1, where n is the total number of elements. Selecting a single element from a NumPy array Each element of these ndarrays can be accessed using its index
2 min read