1
votes

Is there an efficient and/or built in function to remove the all-zero rows of a 2d array? I am looking at numpy documentation but I have not found it.

2

2 Answers

4
votes

Boolean indexing will do it:

In [2]:

a
Out[2]:
array([[4, 1, 1, 2, 0, 4],
       [3, 4, 3, 1, 4, 4],
       [1, 4, 3, 1, 0, 0],
       [0, 4, 4, 0, 4, 3],
       [0, 0, 0, 0, 0, 0]])
In [3]:

a[~(a==0).all(1)]
Out[3]:
array([[4, 1, 1, 2, 0, 4],
       [3, 4, 3, 1, 4, 4],
       [1, 4, 3, 1, 0, 0],
       [0, 4, 4, 0, 4, 3]])