![]() |
VOOZH | about |
In this article, we will discuss how to remove specific elements from the NumPy Array.
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.
Program to create an array with 5 elements and delete the 1st element.
Output:
[1 2 3 4 5] remaining elements after deleting 1st element [2 3 4 5]
Program to create an array with 5 elements and delete the 1st and last element.
Output:
[1 2 3 4 5] remaining elements after deleting 1st and last element [2 3 4]
Removing 8 values from an array.
Output:
[1 2 3 4 5 6 7]
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.
Output:
[9 8 7 6 5 3 2 1]
Deleting 1st column.
Output:
[[ 1 3 4] [ 5 7 8] [ 9 11 12]]
Deleting 1st row.
Output:
[[ 1 2 3 4] [ 9 10 11 12]]
Removing multiple elements from a 2D array. Here, we are removing 1st and last column from an array.
Output:
[[ 2 3] [ 6 7] [10 11]]