6

I have a numpy ndarray that looks like:

[[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[225 224 228],
[163 164 174],
[205 212 229],
[116 130 153],
[ 81 101 132],
[ 34  56  92],
[  2  16  35],
[ 33  44  64],
[ 38  49  71],
[ 63  75  99],
[ 76  88 116],
[ 45  62  95],
[ 29  50  88],
[ 18  40  82],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0]]

I want to delete all the zero elements i.e. [0,0,0]. How can I do this?

1

3 Answers 3

11

You can use np.delete() and np.where() to get the elements where the condition is satisfied as :

del_arr = np.delete(arr, np.where(arr == [0, 0, 0]), axis=0)
print del_arr
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

import numpy as np

array = np.array([  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[225 224 228],
[163 164 174],
[205 212 229],
[116 130 153],
[ 81 101 132],
[ 34  56  92],
[  2  16  35],
[ 33  44  64],
[ 38  49  71],
[ 63  75  99],
[ 76  88 116],
[ 45  62  95],
[ 29  50  88],
[ 18  40  82],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0])


zeros = np.all(np.equal(a, 0), axis=1)
array[~zeros]

Comments

0
data[(data[:,1] & data[:, 2] & data[:, 0]).astype(bool)]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.