note.nkmk.me

numpy.delete(): Delete rows and columns

Posted: 2019-05-29 / Tags: Python, NumPy

Using the NumPy function np.delete(), you can delete any row and column from the NumPy array ndarray.

Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.

This post describes the following contents.

  • Basic usage of np.delete()
    • Specify the index (row / column number): obj
    • Specify the axis (dimension): axis
  • Delete multiple rows and columns at once
    • Use a list
    • Use a slice
    • Delete rows and columns
  • Example for multidimensional arrays

If you want to delete elements, rows and columns according to the condition instead of specifying the position, refer to the following post.

Use reshape() to change the shape.

Basic usage of np.delete()

np.delete() takes three parameters as follows.

  • numpy.delete(arr, obj, axis=None)
    • arr: Input array
    • obj: Row or column number to delete
    • axis: Axis to delete

For example, to delete the second row, set obj=1, axis=0. Details will be described later. The original ndarray is not changed, and a new copy of ndarray is returned.

import numpy as np

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

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]]

Specify the index (row / column number): obj

Specify the index (row number or column number) to be deleted in the second parameter obj. Index starts at 0.

Specifying a nonexistent index raises an error.

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

Specify the axis (dimension): axis

Specify the axis (dimension) to be deleted in the third parameter axis. Axis number starts from 0.

In the case of a two-dimensional array, the row is the first dimension (axis=0) and the column is the second dimension (axis=1).

Specifying a nonexistent dimension raises an error.

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

With axis=None, the element at the index specified by obj is deleted after flattening to one dimension. The default for axis is None.

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]
Sponsored Link

Delete multiple rows and columns at once

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

Use a list

Specify the row numbers and column numbers to be deleted in a list or array.

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

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

Use a slice

It is also possible to specify multiple rows and columns by using a slice specifying a range with [start:stop:step].

For more information about the basics of slicing and slice objects with slice(), see the following post:

slice()

Create a slice object with slice() and specify it as the second parameter 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. If you want to omit, specify None explicitly.

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_[]

Use numpy.s_[] if you want to write 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 rows and columns

You can not delete multiple dimensions (such as rows and columns) at once with np.delete(). If you want to delete different dimensions, repeat np.delete().

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

Example for multidimensional arrays

So far, for the sake of convenience, it has been described in terms of row and column, but the concept is the same in the case of three or more dimensions.

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 dimensions in axis and index in obj.

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]]]

Multiple specifications by list or slice can be written in the same way.

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]]]
Sponsored Link
Share

Related Categories

Related Posts