1

I have a numpy array

my_array = np.array([[1,2,3,4],[5,6,7,8],[0,0,0,0],[1,2,3,4],[0,0,0,0],[0,0,0,1]])

and I would like to get all index when array contains only zero values :

index 2 -> [0,0,0,0]
index 4 -> [0,0,0,0]

Discussion with the similar problem exists : Find indices of elements equal to zero in a NumPy array

but in this solution we get values equal to zero, instead of get array with zero as I want.

Thank for your help.

mnekkach
  • 191
  • 2
  • 11

1 Answers1

1

You can use np.argwhere with np.all to get indices of rows where all elements == 0:

In [11] np.argwhere((my_array == 0).all(axis=1))
Out[11]: 
array([[2],
       [4]], dtype=int64)

Or np.nonzero instead of np.argwhere gives slightly nicer output:

In [12] np.nonzero((my_array == 0).all(axis=1))
Out[12]: (array([2, 4], dtype=int64),)
sjw
  • 6,213
  • 2
  • 24
  • 39