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.

1 Answer 1

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),)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.