In this article we will discuss how to remove elements , rows and columns from 1D & 2D numpy array using np.delete().

np.delete()

Python’s Numpy library provides a method to delete elements from a numpy array based on index position i.e.

numpy.delete(arr, obj, axis=None)

Args:

  • arr : Numpy array
  • obj : index position or list of index positions to be deleted from numpy array arr
  • axis : Axis along which we want to delete. If 1 then dlete columns, if 0 then delete rows and if None then apply delete on flattened array.

Returns:

  • Returns a copy of array arr by deleting elements at index positions pointed by obj. If axis is None then returns a flattened array.

Let’s see how to use np.delete() to remove elements by index positions from 1D & 2D numpy arrays and also how to delete rows & columns from 2D numpy arrays.

First of all import numpy module i.e.

import numpy as np

Delete an element in Numpy Array by Index position

Suppose we have a numpy array of numbers i.e.

# Create a Numpy array from list of numbers
arr = np.array([4,5,6,7,8,9,10,11])

Now let’s delete the element from above numpy array at index position 2 i.e.
# Delete element at index position 2
arr = np.delete(arr, 2)

print('Modified Numpy Array by deleting element at index position 2')
print(arr)

Output:
Modified Numpy Array by deleting element at index position 2
[ 4  5  7  8  9 10 11]

In np.delete() we passed the numpy array and also the index position that we want to be deleted.

Delete multiple element in Numpy Array by Index position

To delete multiple elements from a numpy array by index positions, pass the numpy array and list of index positions to be deleted to np.delete() i.e.

# Create a Numpy array from list of numbers
arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])

# Delete element at index positions 1,2 and 3
arr = np.delete(arr, [1,2,3])

print('Modified Numpy Array by deleting element at index position 1, 2 & 3')
print(arr)

Output:
Modified Numpy Array by deleting element at index position 1, 2 & 3
[ 4  8  9 10 11]

It deletes the elements at index position 1,2 and 3.

Delete rows & columns from a 2D Numpy Array

Suppose we have a 2D numpy array i.e.

# Create a 2D numpy array from list of list
arr2D = np.array([[11 ,12,13,11], [21, 22, 23, 24], [31,32,33,34]])

print(arr2D)

Output:
[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]

Now let’s see how to delete rows and columns from it based on index positions.

Delete a column in Numpy Array by column number

To delete a column from a 2D numpy array using np.delete() we need to pass the axis=1 along with numpy array and index of column i.e.

# Delete column at index 1
arr2D = np.delete(arr2D, 1, axis=1)

print('Modified 2D Numpy Array by removing columns at index 1')
print(arr2D)

Output:
Modified 2D Numpy Array by removing columns at index 1
[[11 13 11]
 [21 23 24]
 [31 33 34]]

It will delete the column at index position 1 from the above created 2D numpy array.

Delete multiple columns in Numpy Array by column number

Pass axis=1 and list of column numbers to be deleted along with numpy array to np.delete() i.e.

# Create a 2D numpy array from list of list
arr2D = np.array([
                    [11, 12, 13, 11],
                    [21, 22, 23, 24],
                    [31, 32, 33, 34]])

# Delete column at index 2 & 3
arr2D = np.delete(arr2D, [2,3], axis=1)

print('Modified 2D Numpy Array by removing columns at index 2 & 3')
print(arr2D)

Output:
Modified 2D Numpy Array by removing columns at index 2 & 3
[[11 12]
 [21 22]
 [31 32]]

It will delete the column at index position 2 and 3 from the above created 2D numpy array.

Delete a row in 2D Numpy Array by row number

Our original 2D numpy array arr2D is,

[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]

To delete a row from a 2D numpy array using np.delete() we need to pass the axis=0 along with numpy array and index of row i.e. row number,
# Delete row at index 0 i.e. first row
arr2D = np.delete(arr2D, 0, axis=0)

print('Modified 2D Numpy Array by removing rows at index 0')
print(arr2D)

Output:
[[21 22 23 24]
 [31 32 33 34]]

It will delete the row at index position 0 from the above created 2D numpy array.

Delete multiple rows in Numpy Array by row number

Our original 2D numpy array arr2D is,

[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]

Pass axis=0 and list of row numbers to be deleted along with numpy array to np.delete() i.e.
# Delete rows at ro1 1 & 2
arr2D = np.delete(arr2D, [1, 2], axis=0)

print('Modified 2D Numpy Array by removing rows at index 1 & 2')
print(arr2D)

Output:
Modified 2D Numpy Array by removing rows at index 1 & 2
[[11 12 13 11]]

It will delete the row at index position 1 and 2 from the above created 2D numpy array.

Delete specific elements in 2D Numpy Array by index position

Our original 2D numpy array arr2D is,

[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]

When we don’t pass axis argument to np.delete() then it’s default value is None, which means 2D numpy array will be flattened for deleting elements at given index position.  Let’s use np.delete() to delete element at row number 0 and column 2 from our 2D numpy array,
# Delete element in row 0 and column 2 from 2D numpy array
modArr = np.delete(arr2D, 2)

print('Modified 2D Numpy Array by removing element at row 0 & column 2')
print(modArr)

Output:
Modified 2D Numpy Array by removing element at row 0 & column 2
[11 12 11 21 22 23 24 31 32 33 34]

It returns the flattened copy of 2D numpy array after deleting element. We passed 2 because in flattened 2d matrix we gor the number from row and column position i.e. position in flattened array = row * no_of_columns + column. So, position in flattened array = 0 * no_of_columns + 2 = 2.

We have created a function to do this calculation and delete element from 2D numpy array by row and column position i.e.

def deleteFrom2D(arr2D, row, column):
    'Delete element from 2D numpy array by row and column position'
    modArr = np.delete(arr2D, row * arr2D.shape[1] + column)
    return modArr

let’s use this to delete element at row 1& column 1 from our 2D numpy array i.e.
# Delete element in row 1 and column 1 from 2D numpy array
modArr = deleteFrom2D(arr2D, 1,1)

print('Modified 2D Numpy Array by removing element at row 1 & column 1')
print(modArr)

Output:
Modified 2D Numpy Array by removing element at row 1 & column 1
[11 12 13 11 21 23 24 31 32 33 34]

It returns the flattened copy of 2D numpy array after deleting element at row 1 and column 1.

Complete example is as follows:

def deleteFrom2D(arr2D, row, column):
    'Delete element from 2D numpy array by row and column position'
    modArr = np.delete(arr2D, row * arr2D.shape[1] + column)
    return modArr



import numpy as np

def main():

    # Create a Numpy array from list of numbers
    arr = np.array([4,5,6,7,8,9,10,11])

    print('Original Array : ', arr)

    print('*** Delete an element in Numpy Array by Index position ***')

    # Delete element at index position 2
    arr = np.delete(arr, 2)

    print('Modified Numpy Array by deleting element at index position 2')
    print(arr)

    print('*** Delete multiple element in Numpy Array by Index position ***')

    # Create a Numpy array from list of numbers
    arr = np.array([4, 5, 6, 7, 8, 9, 10, 11])

    # Delete element at index positions 1,2 and 3
    arr = np.delete(arr, [1,2,3])

    print('Modified Numpy Array by deleting element at index position 1, 2 & 3')
    print(arr)

    print('**** Delete elements from a 2D Numpy Array ****')

    # Create a 2D numpy array from list of list
    arr2D = np.array([[11 ,12,13,11], [21, 22, 23, 24], [31,32,33,34]])

    print('2D Numpy Array : ')
    print(arr2D)

    print('*** Delete a column in Numpy Array by column number *** ')

    # Delete column at index 1
    arr2D = np.delete(arr2D, 1, axis=1)

    print('Modified 2D Numpy Array by removing columns at index 1')
    print(arr2D)

    print('*** Delete multiple columns in Numpy Array by column numbers *** ')

    # Create a 2D numpy array from list of list
    arr2D = np.array([
                        [11, 12, 13, 11],
                        [21, 22, 23, 24],
                        [31, 32, 33, 34]])

    # Delete column at index 2 & 3
    arr2D = np.delete(arr2D, [2,3], axis=1)

    print('Modified 2D Numpy Array by removing columns at index 2 & 3')
    print(arr2D)


    print('**** Delete a row in Numpy Array by Index position **** ')

    # Create a 2D numpy array from list of list
    arr2D = np.array([[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]])

    # Delete row at index 0 i.e. first row
    arr2D = np.delete(arr2D, 0, axis=0)

    print('Modified 2D Numpy Array by removing rows at index 0')
    print(arr2D)

    print('**** Delete multiple rows in Numpy Array by Index positions **** ')

    # Create a 2D numpy array from list of list
    arr2D = np.array([[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]])

    # Delete rows at ro1 1 & 2
    arr2D = np.delete(arr2D, [1, 2], axis=0)

    print('Modified 2D Numpy Array by removing rows at index 1 & 2')
    print(arr2D)

    print('**** Delete a specific element at index position in 2D numpy array ***')

    # Create a 2D numpy array from list of list
    arr2D = np.array([[11, 12, 13, 11], [21, 22, 23, 24], [31, 32, 33, 34]])

    print('Origibal 2D Numpy Array')
    print(arr2D)

    # Delete element in row 0 and column 2 from 2D numpy array
    modArr = np.delete(arr2D, 2)

    print('Modified 2D Numpy Array by removing element at row 0 & column 2')
    print(modArr)

    # Delete element in row 1 and column 1 from 2D numpy array
    modArr = deleteFrom2D(arr2D, 1,1)

    print('Modified 2D Numpy Array by removing element at row 1 & column 1')
    print(modArr)

if __name__ == '__main__':
  main()


Output:
Original Array :  [ 4  5  6  7  8  9 10 11]
*** Delete an element in Numpy Array by Index position ***
Modified Numpy Array by deleting element at index position 2
[ 4  5  7  8  9 10 11]
*** Delete multiple element in Numpy Array by Index position ***
Modified Numpy Array by deleting element at index position 1, 2 & 3
[ 4  8  9 10 11]
**** Delete elements from a 2D Numpy Array ****
2D Numpy Array : 
[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]
*** Delete a column in Numpy Array by column number *** 
Modified 2D Numpy Array by removing columns at index 1
[[11 13 11]
 [21 23 24]
 [31 33 34]]
*** Delete multiple columns in Numpy Array by column numbers *** 
Modified 2D Numpy Array by removing columns at index 2 & 3
[[11 12]
 [21 22]
 [31 32]]
**** Delete a row in Numpy Array by Index position **** 
Modified 2D Numpy Array by removing rows at index 0
[[21 22 23 24]
 [31 32 33 34]]
**** Delete multiple rows in Numpy Array by Index positions **** 
Modified 2D Numpy Array by removing rows at index 1 & 2
[[11 12 13 11]]
**** Delete a specific element at index position in 2D numpy array ***
Origibal 2D Numpy Array
[[11 12 13 11]
 [21 22 23 24]
 [31 32 33 34]]
Modified 2D Numpy Array by removing element at row 0 & column 2
[11 12 11 21 22 23 24 31 32 33 34]
Modified 2D Numpy Array by removing element at row 1 & column 1
[11 12 13 11 21 23 24 31 32 33 34]

 

Join a list of 2000+ Programmers for latest Tips & Tutorials