The Wayback Machine - https://web.archive.org/web/20240625050420/https://www.geeksforgeeks.org/how-to-remove-specific-elements-from-a-numpy-array/
Open In App

How to remove specific elements from a NumPy array ?

Last Updated : 12 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

NumPy ndarray.size() Method | Get Number of Elements in NumPy Array
The ndarray.size() method returns the number of elements in the NumPy array. It works the same as np.prod(a.shape), i.e., the product of the dimensions of the array. Example C/C++ Code import numpy as np arr = np.zeros((3, 4, 2), dtype = np.complex128) gfg = arr.size print (gfg) Output : 24Syntax Syntax: numpy.ndarray.size(arr) Parameters arr : [ar
1 min read
NumPy ndarray.__abs__() | Find Absolute Value of Elements in NumPy Array
The ndarray.__abs__() method returns the absolute value of every element in the NumPy array. It is automatically invoked when we use Python's built-in method abs() on a NumPy array. Example C/C++ Code import numpy as np gfg = np.array([1.45, 2.32, 3.98, 4.41, 5.55, 6.12]) print(gfg.__abs__()) Output[ 1 2 3 4 5 6] SyntaxSyntax: ndarray.__abs__() Ret
1 min read
NumPy ndarray.__ilshift__() | Shift NumPy Array Elements to Left
The ndarray.__ilshift__() method is an in-place left-shift operation. It shifts elements in the array to the left of the number of positions specified. Example C/C++ Code import numpy as np gfg = np.array([1, 2, 3, 4, 5]) # applying ndarray.__ilshift__() method print(gfg.__ilshift__(2)) Output[ 4 8 12 16 20] SyntaxSyntax: ndarray.__ilshift__($self,
1 min read
NumPy ndarray.__irshift__() | Shift NumPy Array Elements to Right
The ndarray.__irshift__() method returns a new array where each element is right-shifted by the value that is passed as a parameter. Example C/C++ Code import numpy as np gfg = np.array([1, 2, 3, 4, 5]) # applying ndarray.__irshift__() method print(gfg.__irshift__(2)) Output[0 0 0 1 1] SyntaxSyntax: ndarray.__irshift__($self, value, /) Parameter se
1 min read
Flatten Specific Dimensions of NumPy Array
A general-purpose array-processing package that is used for working with arrays is called NumPy. Do you want to collapse your Numpy array into one dimension? If yes, then you can do so by flattening your Numpy array. In this article, we will see how we can flatten only some dimensions of a Numpy array. Flatten only some dimensions of a NumPy ArrayU
2 min read
NumPy ndarray.__eq__() Method | Compare Array Values to Specific Value
The ndarray.__eq__() method of Numpy compares the values in ndarray to a specific value. It is useful to find which value in ndarray is equal to the given value. It will return you a NumPy array with a boolean type having only values True and False. Example C/C++ Code # import the important module in python import numpy as np # make an array with n
2 min read
Remove elements larger than a specific value from a list in Python
In this article, we will learn to remove elements from a list that is larger than a specific value in Python. Example Input: [12, 33, 10, 20, 25], value = 21 Output: [12, 10, 20] Explanation: Removed all element from the list that are greater than 21. Remove list elements greater than a given value using list comprehensionIn the following example,
4 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read
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 our input NumPy ArrayInput2: [ [1 2 3 4 5] [0 1 2 9
7 min read
Count the number of days of a specific month using NumPy
In order to count the number of days of a specific month, we will be using numpy.datetime64() method of numpy module. Examples: Input: month = 2; year = 2016 Output: 29 days Input: month = 12; year = 2020 Output: 31 days Let us assume the month whose number of days we want to count be P and the next month is N. We will subtract the first date of th
3 min read
Practice Tags :
three90RightbarBannerImg