How to Delete Rows from a NumPy Array

In this article, we will learn how to remove single or multiple rows from a 2D NumPy Array in Python.

Table of Contents

Given a 2D NumPy array we need to delete the specified rows from it. For Example:

Given 2D NumPy Array:

[[1, 2, 3, 4, 5],
 [5, 4, 3, 2, 1],
 [8, 2, 4, 1, 5], 
 [3, 4, 3, 2, 1], 
 [7, 6, 3, 4, 5]]

After removing 2nd row:

[[1, 2, 3, 4, 5],
 [8, 2, 4, 1, 5], 
 [3, 4, 3, 2, 1], 
 [7, 6, 3, 4, 5]]

There are multiple ways to delete rows from a NumPy Array. Lets discuss all the methods one by one with proper approach and a working code example.

1.) Remove Single Row from NumPy Array using delete()

The delete() method is a built-in method in numpy library and it provides a way to delete the elements from the given array. The delete() method takes an array and an index position or array of indices as parameters. It returns an array by deleting the elements at given index.

Now to delete a row in the array we need to pass the given array and the index of the row and axis=0 to the delete() method. In this example, to delete the 2nd row from the given array, call the function delete(a, 1, axis=0) . Here, index of 2nd row is 1.

Syntax of delete()

numpy.delete(arr, obj, axis)

Parameters:

        arr          = The array to be passed to the function.
        obj          = index (or array of index)  of the rows or columns to be deleted.
        axis         = Axis along which we want to delete. 
                       If axis is 1 then delete columns, if axis is 0 then delete rows.
                       If None then flatten the array and then apply delete on it.

Return:

If axis is 1, then it returns an array with the specified columns removed.

Approach

1. Import numpy library and create numpy array
2. Now pass the given array and the index of row to be removed to the delete() method.
3. Set the axis = 0 in the delete() method.
4. The method will return the array with the specified row deleted.
5. print the array.

Source code

import numpy as np

# Create 2D NumPy Array
a = np.array([[1, 2, 3, 4, 5],
              [5, 4, 3, 2, 1],
              [8, 2, 4, 1, 5],
              [3, 4, 3, 2, 1]])

print('Original Array:')
print(a)

# Delete 2nd row from the 2D NumPy Aray
# passing index as 1 and setting axis=0
a = np.delete(a, 1, axis=0)

print('After removing 2nd row of Array:')
print(a)

OUTPUT:

Original Array:

[[1 2 3 4 5]
 [5 4 3 2 1]
 [8 2 4 1 5]
 [3 4 3 2 1]]

After removing 2nd row of Array:

[[1 2 3 4 5]
 [8 2 4 1 5]
 [3 4 3 2 1]]

2.) Remove Multiple Rows from NumPy Array using delete()

The delete() method is a built-in method in numpy library and it is used to remove the rows from the given array.

numpy.delete(arr, obj, axis)

The delete() method takes an array and a index position or array of index positions as parameters. It returns an array by deleting the elements at given index or indices. Now to remove multiple rows in the array we need to pass the given array and the ‘array of indices’ of the rows to be deleted and axis=0 to the delete() method. In this example, lets delete the 1st and 2nd row from the given array. For that we can call delete() function with following arguments –> delete(a, [0,1], axis=0). Here, we passed list of indexes of 1st and 2nd row along with axis 0 to the delete() function.

Approach

1. Import numpy library and create numpy array
2. Now use pass the given array and the array of index of rows to be deleted to the delete() method.
3. Set the axis = 0 in the delete() method.
4. The method will return the array with the specified rows deleted.
5. print the array.

Source code

import numpy as np

# Create 2D NumPy Array
a = np.array([[1, 2, 3, 4, 5],
              [5, 4, 3, 2, 1],
              [8, 2, 4, 1, 5],
              [3, 4, 3, 2, 1]])

print('Original Array:')
print(a)

# Delete 1st and 2nd rows from the 2D NumPy Aray
# passing index as 1 and setting axis=0
a = np.delete(a, [0,1], axis=0)

print('After removing 1st and 2nd row of Array:')
print(a)

OUTPUT:

Original Array:
[[1 2 3 4 5]
 [5 4 3 2 1]
 [8 2 4 1 5]
 [3 4 3 2 1]]

After removing 2nd row of Array:
[[8 2 4 1 5]
 [3 4 3 2 1]]

3.) Remove Multiple Rows from NumPy Array using slicing

Slicing in python can be defined as selecting elements from array by range i.e. selecting from one given index to another given index position.

Example:             
        arr = [ 1, 3, 5, 8, 9 ]
        arr[ start : stop ]
        arr[0 : 2]  ===> this will give [ 1, 3], The stop index will be excluded.
    

The delete() method is a built-in method in numpy library and it is used to remove the rows from the given array. The delete() method takes an array and an index position or array of index parameters. It returns an array by deleting the elements at given index or indices.
Now to remove multiple rows in the array we need to pass the given array and the ‘array of index’ of the rows to be deleted and axis=0 to the delete() method. Slice() method is used to pass the slicing to the delete() method.

Syntax of slice()

slice(start, end)

Parameters:

start        = The start of index
end          = The end of index, last index will be excluded.

In this example let’s delete the 1st row and 2nd row from the given array. For that we will call delete(a, slice(0,3), axis=0). Pass the indices from 1st to 3rd row in delete() function. This will delete the 1st and 2nd row because the index of 3rd row is excluded in slicing.

Approach

1. Import numpy library and create numpy array
2. Create a slice object using slice()
2. Now use pass the given array and the slice object to the delete() method.
3. Set the axis = 0 in the delete method.
4. The method will return the array with the specified rows deleted.
5. print the array.

Source code

import numpy as np

# Create 2D NumPy Array
a = np.array([[1, 2, 3, 4, 5],
              [5, 4, 3, 2, 1],
              [8, 2, 4, 1, 5],
              [3, 4, 3, 2, 1]])

print('Original Array:')
print(a)

# deletes the first 2 rows.
a = np.delete(a, slice(0,2), axis=0)

print('After removing first two rows of NumPy Array:')
print(a)

OUTPUT:

Original Array:

[[1 2 3 4 5]
 [5 4 3 2 1]
 [8 2 4 1 5]
 [3 4 3 2 1]]


After removing first two rows of NumPy Array:

[[8 2 4 1 5]
 [3 4 3 2 1]]

4.) Remove Multiple Rows from NumPy Array using Boolean Array

The rows in a 2D NumPy Array can be accessed by passing a boolean array as an index to the array

Example:

arr = [ [1, 2, 3, 4, 5],
        [5, 4, 3, 2, 1],
        [8, 2, 4, 1, 5],
        [3, 4, 3, 2, 1],
        [7, 6, 3, 4, 5]]
             
boolArray = [True, True, False, False, False]

arr[boolArray]  ===> this will give,

[[1, 2, 3, 4, 5],
 [5, 4, 3, 2, 1]]
    

Now to remove rows from the 2D NumPy Array, create a Boolean array with length same as number of rows in the 2D NumPy Array. For rows to be deleted, set False at corresponding index in Boolean array and True at other positions. Pass this Boolean array as index to the given array. This will return an array with the specified rows deleted.

Approach

  • Import numpy library and create numpy array
  • Create a Boolean array with length same as number of rows in 2D NumPy Array.
  • For rows to be deleted, set False at corresponding index in Boolean array and True at other positions.
  • Now Pass this Boolean array as index to the given array
  • This will give an array with the specified rows deleted.

Source code

import numpy as np

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

print('Original Array:')
print(a)

# Removing the rows using boolean index
booleanIndex = [False, False, False,True, True]
a = a[booleanIndex]

print('After removing last two rows of 2D Array:')
print(a)

OUTPUT:

Original Array:

[[1 2 3 4 5]
 [5 4 3 2 1]
 [8 2 4 1 5]
 [3 4 3 2 1]
 [7 6 3 4 5]]

After removing last two rows of 2D Array:

[[3 4 3 2 1]
 [7 6 3 4 5]]

Summary

Great! you made it. We have discussed all possible methods to delete rows from a NumPy Array. Happy learning.

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