NumPy: Delete rows/columns from an array with np.delete()

Modified: | Tags: Python, NumPy

In NumPy, the np.delete() function allows you to delete specific rows, columns, and other elements from an array (ndarray).

Users must specify the target axis (dimension) and the positions (such as row or column numbers) to be deleted. Additionally, it is possible to delete multiple rows or columns simultaneously using a list or a slice.

Refer to the following articles to learn how to delete elements, rows, and columns based on conditions rather than by specifying their positions by index, or to remove rows and columns that contain NaN.

To change the shape, such as reducing or increasing the dimensions, use reshape().

The NumPy version used in this article is as follows. Note that functionality may vary between versions.

import numpy as np

print(np.__version__)
# 1.26.1

Basic usage of np.delete()

The np.delete() function requires three arguments.

  • np.delete(arr, obj, axis=None)
    • arr: The input array
    • obj: The row or column numbers to delete, specified as an integer, list, or slice
    • axis: The axis to delete from, default is None

Consider the following 2D array as an example.

a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

For example, to delete the second row, set obj=1 (zero-based indexing) and axis=0. The original ndarray is not changed, and a new copy of ndarray is returned.

a_del = np.delete(a, 1, 0)
print(a_del)
# [[ 0  1  2  3]
#  [ 8  9 10 11]]

print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

Specifying the index to delete: obj

The second argument obj specifies the index (such as row or column numbers) to delete. Specifying a non-existent index will result in an error.

The target axis is specified by the third argument axis. In this example, axis=0 targets rows.

a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

print(np.delete(a, 0, 0))
# [[ 4  5  6  7]
#  [ 8  9 10 11]]

print(np.delete(a, 2, 0))
# [[0 1 2 3]
#  [4 5 6 7]]

# print(np.delete(a, 3, 0))
# IndexError: index 3 is out of bounds for axis 0 with size 3

Specifying the axis to delete: axis

The third argument axis specifies the axis from which to delete elements. Specifying a non-existent dimension will result in an error.

In a 2D array, rows correspond to the 0th dimension and columns to the 1st dimension.

a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

print(np.delete(a, 1, 0))
# [[ 0  1  2  3]
#  [ 8  9 10 11]]

print(np.delete(a, 1, 1))
# [[ 0  2  3]
#  [ 4  6  7]
#  [ 8 10 11]]

# print(np.delete(a, 1, 2))
# AxisError: axis 2 is out of bounds for array of dimension 2

If axis=None, the array is flattened before deleting the elements specified by obj. Because the default value for axis is None, omitting this argument will automatically trigger this behavior.

print(np.delete(a, 1, None))
# [ 0  2  3  4  5  6  7  8  9 10 11]

print(np.delete(a, 1))
# [ 0  2  3  4  5  6  7  8  9 10 11]

Delete multiple rows/columns at once

Multiple rows and columns can be deleted at once by specifying a list or a slice in the second argument obj.

Specify with a list

Specify the row or column numbers with a list.

a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

print(np.delete(a, [0, 2], 0))
# [[4 5 6 7]]

print(np.delete(a, [0, 3], 1))
# [[ 1  2]
#  [ 5  6]
#  [ 9 10]]

Starting from NumPy version 1.19, a list or array of Boolean values can be treated as a mask, with the indexes corresponding to True being deleted. An error occurs if the specified number of elements does not match the size of the dimension.

print(np.delete(a, [True, False, True], 0))
# [[4 5 6 7]]

print(np.delete(a, [True, False, False, True], 1))
# [[ 1  2]
#  [ 5  6]
#  [ 9 10]]

# print(np.delete(a, [True, False, True], 1))
# ValueError: boolean array argument obj to delete must be one dimensional and match the axis length of 4

Specify with a slice

It is also possible to use a slice [start:stop:step] to specify multiple rows or columns. For more details on slices and slice(), refer to the following article.

slice()

Generate a slice object with slice(), and specify it as the second argument obj.

It is equivalent to [:stop] if there is only one argument, [start:stop] if there are two, and [start:stop:step] if there are three. Use None explicitly to omit.

a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

print(np.delete(a, slice(2), 1))
# [[ 2  3]
#  [ 6  7]
#  [10 11]]

print(np.delete(a, slice(1, 3), 1))
# [[ 0  3]
#  [ 4  7]
#  [ 8 11]]

print(np.delete(a, slice(None, None, 2), 1))
# [[ 1  3]
#  [ 5  7]
#  [ 9 11]]

np.s_[]

numpy.s_[] allows for writing slices in the form [start:stop:step].

print(np.delete(a, np.s_[:2], 1))
# [[ 2  3]
#  [ 6  7]
#  [10 11]]

print(np.delete(a, np.s_[1:3], 1))
# [[ 0  3]
#  [ 4  7]
#  [ 8 11]]

print(np.delete(a, np.s_[::2], 1))
# [[ 1  3]
#  [ 5  7]
#  [ 9 11]]

Delete both rows and columns at once

It is impossible to delete multiple dimensions (such as both rows and columns) at once with np.delete(). To delete different dimensions, apply delete() repeatedly.

a = np.arange(12).reshape(3, 4)
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

print(np.delete(np.delete(a, 1, 0), 1, 1))
# [[ 0  2  3]
#  [ 8 10 11]]

Examples with three or more dimensions

So far, for convenience, we have discussed rows and columns of two-dimensional arrays, but the same concepts apply to arrays with three or more dimensions.

Consider the following array as an example.

a_3d = np.arange(24).reshape(2, 3, 4)
print(a_3d)
# [[[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]
# 
#  [[12 13 14 15]
#   [16 17 18 19]
#   [20 21 22 23]]]

print(a_3d.shape)
# (2, 3, 4)

Specify the dimension with the axis argument and the indexes with the obj argument.

print(np.delete(a_3d, 1, 0))
# [[[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]]

print(np.delete(a_3d, 1, 1))
# [[[ 0  1  2  3]
#   [ 8  9 10 11]]
# 
#  [[12 13 14 15]
#   [20 21 22 23]]]

print(np.delete(a_3d, 1, 2))
# [[[ 0  2  3]
#   [ 4  6  7]
#   [ 8 10 11]]
# 
#  [[12 14 15]
#   [16 18 19]
#   [20 22 23]]]

The same applies to specifying multiple indexes with a list or slice.

print(np.delete(a_3d, [0, 3], 2))
# [[[ 1  2]
#   [ 5  6]
#   [ 9 10]]
# 
#  [[13 14]
#   [17 18]
#   [21 22]]]

print(np.delete(a_3d, np.s_[::2], 2))
# [[[ 1  3]
#   [ 5  7]
#   [ 9 11]]
# 
#  [[13 15]
#   [17 19]
#   [21 23]]]

Related Categories

Related Articles