Remove elements from NumPy Array by Index

In this article, we will learn how to remove elements from a Numpy Array by index position.

Table Of Contents

Given a NumPy array we need to delete the element at the given index of the array.

Example:             
    Given array = [1 2 3 4 5 6 7]
    After removing element at index position 2 i.e. the third element: [1 2 4 5 6 7]

There are multiple ways to remove elements from Numpy Array by Index. Lets discuss all the methods one by one with proper approach and a working code example.

Using delete() method to delete elements from Numpy Array by Index

The delete() method is a built-in method in Numpy library and it is used to remove the elements from NumpY Array based on index positions. The delete() method takes following arguments,

  • A NumPy Array from which we need to delete the elements
  • An index position or an array of indices at which elements need to be deleted.

To delete the 3rd element in the array, to do this we need to pass the given array and the index of the 3rd element to the delete() method. The index of the 3rd element is 2. It will return a new NumPy Array with the elements removed.

Approach

  1. Import numpy library and create a NumPy Array
  2. Now pass the given array and the index of 3rd element i.e. 2, to the delete() method.
  3. Print the array.

Source code

import numpy as np

# creating numpy array
arr = np.array([1,2,3,4,5,6,7])


# The INDEX of Third element is 2.
index = 2

# Removing the Third element using delete() method
arr = np.delete(arr, index)

print(arr)

OUTPUT:

[1 2 4 5 6 7]

It removed the element at index position2 from the NumPy Array.

Using delete() method to delete multiple elements from NumPy Array

The delete() method is a built-in method in numpy library and it helps in removing the elements from NumPy array based on index positions. In the previous example we deleted only one element by its index position using the delete() function. Now let’s see how we can delete multiple elements from NumPy Array based on index positions.

Approach

  1. Import numpy library and create a NumPy array
  2. Create a list which contains the index positions of elements to be deleted.
  3. Now pass the given array and the index list to the delete() method.
  4. Print the array.

Source code

import numpy as np

# Create a Numpy Array
arr = np.array([1,2,3,4,5,6,7])

# List of indices of elements that need to be deleted
indexList = [1, 2, 4]

# Removing multiple elements based on index positions
arr = np.delete(arr, indexList)

print(arr)

Output:

[1 4 6 7]

It deleted alements at index position 1, 2 and 4 from the NumPy Array.

Using boolean array to delete elements from NumPy Array by index positions

The elements in a numpy array can be accesed by passing a boolean array as index to the array

Example:             

    arr = [ 1, 3, 5, 8, 9 ]
    boolArray = [True, True, False, False, False]
    arr[boolArray]  ===> this will give [ 1, 3 ]

Now to remove a element from the array create a boolean array with length same as the array and make all the elements as True except for the element that needs to be deleted. Pass this boolean array as index to the given array. This will give an array with the element removed.

Approach

  1. import numpy library and create numpy array
  2. Create a boolean array with length same as the array and make all the elements as True except for the element to be deleted
  3. Now Pass this boolean array as index to the Given array.
  4. This will give an array with the element removed.

Source code

import numpy as np

# creating numpy array
arr = np.array([1,2,3,4,5,6,7])

# INDEX of Third element is 2.
index = 2

booleanIndex = [True for i in arr]
booleanIndex[index] = False

# Removing the 3rd element using boolean index
arr = arr[booleanIndex]

print(arr)

OUTPUT:

[1 2 4 5 6 7]

It removed the element at index position 2 from the NumPy Array.

Remove elements from NumPy Array Using index array

The elements in a numpy array can be accesed by passing a index array as index to the array

Example:

    arr = [ 1, 3, 5, 8, 9 ]
    indexArray = [1,3]
    arr[indexArray]  ===> this will give [ 3, 8 ]

Now to remove the element from the array, create an index array with indexes of all the elements except for the elements that need to be deleted. Pass this index array as index to the given array. This will give an array with the element removed.

Source code

import numpy as np

# creating numpy array
arr = np.array([1,2,3,4,5,6,7])

#INDEX of Third element is 2.
index = 2

# Removing the 3rd element using index array
indexArray = [i for i in range(0, len(arr))]
indexArray.remove(index)

arr = arr[indexArray]

print(arr)

Output:

[1 2 4 5 6 7]

It removed the element at index position 2 from the NumPy Array.

Remove elements from NumPy Array by index using setdiff1d() method

The setdiff1d() method is a built-in method in numpy library. It takes two arrays as input and finds difference of two arrays. It returns the values in array 1, that are not in array 2. Now to delete an element from NumPy Array based on index position pass the following arguments to setdiff1d() function,

  • The given NumPy
  • The Array’s 3rd position as 2nd array.

The setdiff1d() method returns the array with the 3rd element deleted.

Source code

import numpy as np

# creating numpy array
arr = np.array([11, 22, 33, 44, 55, 66, 77])

# INDEX of Third element is 2.
index = 2

# Delete element at index position 2
arr = np.setdiff1d(arr, [arr[2]])

print(arr)

Output:

[11 22 44 55 66 77]

Summary

Great! you made it, We have disussed All possible methods to Remove elements from Numpy Array by Index. Happy learning, You can find amazing and interesting articles like this here

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