15

How can I delete multiple rows of NumPy array? For example, I want to delete the first five rows of x. I'm trying the following code:

import numpy as np
x = np.random.rand(10, 5)
np.delete(x, (0:5), axis=0)

but it doesn't work:

np.delete(x, (0:5), axis=0)
               ^
SyntaxError: invalid syntax

3 Answers 3

25

There are several ways to delete rows from NumPy array.

The easiest one is to use basic indexing as with standard Python lists:

>>> import numpy as np
>>> x = np.arange(35).reshape(7, 5)
>>> x
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])
>>> result = x[5:]
>>> result
array([[25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])

You can select not only rows but columns as well:

>>> x[:2, 1:4]
array([[1, 2, 3],
       [6, 7, 8]])

Another way is to use "fancy indexing" (indexing arrays using arrays):

>>> x[[0, 2, 6]]
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14],
       [30, 31, 32, 33, 34]])

You can achieve the same using np.take:

>>> np.take(x, [0, 2, 6], axis=0)
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14],
       [30, 31, 32, 33, 34]])

Yet another option is to use np.delete as in the question. For selecting the rows/columns for deletion it can accept slice objects, int, or array of ints:

>>> np.delete(x, slice(0, 5), axis=0)
array([[25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])
>>> np.delete(x, [0, 2, 3], axis=0)
array([[ 5,  6,  7,  8,  9],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])

But all this time that I've been using NumPy I never needed this np.delete, as in this case it's much more convenient to use boolean indexing.

As an example, if I would want to remove/select those rows that start with a value greater than 12, I would do:

>>> mask_array = x[:, 0] < 12  # comparing values of the first column
>>> mask_array
array([ True,  True,  True, False, False, False, False])
>>> x[mask_array]
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> x[~mask_array]  # ~ is an element-wise inversion
array([[15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34]])

For more information refer to the documentation on indexing: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

Sign up to request clarification or add additional context in comments.

Comments

8

If you want to delete selected rows you can write like

np.delete(x, (1,2,5), axis = 0)

This will delete 1,2 and 5 th line, and if you want to delete like (1:5) try this one

np.delete(x, np.s_[0:5], axis = 0)

by this you can delete 0 to 4 lines from your array.

np.s_[0:5] --->> slice(0, 5, None) both are same.

Comments

0

Pass the multiple row numbers to the list argument. General Syntax:

np.delete(array_name,[rownumber1,rownumber2,..,rownumber n],axis=0)

Example: delete first three rows in an array:

np.delete(array_name,[0,1,2],axis=0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.