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.
Asked
Active
Viewed 7,423 times
2 Answers
4
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]])

CT Zhu
- 52,648
- 17
- 120
- 133
-1
You can use the built-in function numpy.nonzero
.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html

Pythontology
- 1,494
- 2
- 12
- 15