user1220022
user1220022

Reputation: 12105

Remove one value from a NumPy array

I am trying to all rows that only contain zeros from a NumPy array. For example, I want to remove [0,0] from

n = np.array([[1,2], [0,0], [5,6]])

and be left with:

np.array([[1,2], [5,6]])

Upvotes: 5

Views: 30554

Answers (4)

Trucy Luce
Trucy Luce

Reputation: 21

To delete according to value,which is an Object.
To do like this:

>>> n
array([[1, 2],
       [0, 0],
       [5, 6]])
>>> bl=n==[0,0]
>>> bl
array([[False, False],
       [ True,  True],
       [False, False]], dtype=bool)
>>> bl=np.any(bl,axis=1)
>>> bl
array([False,  True, False], dtype=bool)
>>> ind=np.nonzero(bl)[0]
>>> ind
array([1])
>>> np.delete(n,ind,axis=0)
array([[1, 2],
       [5, 6]])

Upvotes: 2

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94595

If you want to delete any row that only contains zeros, the fastest way I can think of is:

n = numpy.array([[1,2], [0,0], [5,6]])

keep_row = n.any(axis=1)  # Index of rows with at least one non-zero value
n_non_zero = n[keep_row]  # Rows to keep, only

This runs much faster than Simon's answer, because n.any() stops checking the values of each row as soon as it encounters any non-zero value (in Simon's answer, all the elements of each row are compared to zero first, which results in unnecessary computations).


Here is a generalization of the answer, if you ever need to remove a rows that have a specific value (instead of removing only rows that only contain zeros):

n = numpy.array([[1,2], [0,0], [5,6]])

to_be_removed = [0, 0]  # Can be any row values: [5, 6], etc.
other_rows = (n != to_be_removed).any(axis=1)  # Rows that have at least one element that differs
n_other_rows = n[other_rows]  # New array with rows equal to to_be_removed removed.

Note that this solution is not fully optimized: even if the first element of to_be_removed does not match, the remaining row elements from n are compared to those of to_be_removed (as in Simon's answer).

I'd be curious to know if there is a simple efficient NumPy solution to the more general problem of deleting rows with a specific value.

Using cython loops might be a fast solution: for each row, element comparison could be stopped as soon as one element from the row differs from the corresponding element in to_be_removed.

Upvotes: 4

Simon Bergot
Simon Bergot

Reputation: 10592

To remove the second row from a numpy table:

import numpy
n = numpy.array([[1,2],[0,0],[5,6]])
new_n = numpy.delete(n, 1, axis=0)

To remove rows containing only 0:

import numpy
n = numpy.array([[1,2],[0,0],[5,6]])
idxs = numpy.any(n != 0, axis=1) # index of rows with at least one non zero value
n_non_zero = n[idxs, :] # selection of the wanted rows

Upvotes: 10

Arjun
Arjun

Reputation: 1279

You can use numpy.delete to remove specific rows or columns.

For example:

n = [[1,2], [0,0], [5,6]]

np.delete(n, 1, axis=0)

The output will be:

array([[1, 2],
       [5, 6]])

Upvotes: 3

Related Questions