0

Say if I have an array:

b=np.arange(10,40).reshape(10,3)
b[3,0]=b[6,1]=b[7,2]=0

array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18],
       [ 0, 20, 21],
       [22, 23, 24],
       [25, 26, 27],
       [28,  0, 30],
       [31, 32,  0],
       [34, 35, 36],
       [37, 38, 39]])

What is the easiest way to remove the rows from the array which have any element = 0?

The closest answer I've found seems to be this: Removing rows in NumPy efficiently

Community
  • 1
  • 1
user2974839
  • 187
  • 1
  • 11

1 Answers1

4

Easiest? Maybe

>>> b = b[~(b == 0).any(axis=1)]
>>> b
array([[10, 11, 12],
       [13, 14, 15],
       [16, 17, 18],
       [22, 23, 24],
       [25, 26, 27],
       [34, 35, 36],
       [37, 38, 39]])

which keeps all the rows except those which have any element equal to zero. This won't necessarily be the fastest, but I'd be very surprised if this were the bottleneck in your code, and it's very easy to spend more time optimizing than you'd ever save.

DSM
  • 342,061
  • 65
  • 592
  • 494