I have a numpy array as follows:
array([ True, True, True, True, True, False, True, True, False,
True, False, True, True, True, True, True, True, False,
False, False, False, False, True, True, False, False, False,
True, True, True, True, True, True, True, False, True,
True, True, True, False, True, True, False, False, True,
True, True, False, True, True, True, False],
I want to get the indices of all the True elements. There is no get_loc method in numpy like Pandas Series and similarly no index method like a list. I don't want to convert it into a list and then use .index.
Any idea?
Use ndarray.nonzero:
>>> a.nonzero()
(array([ 0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27,
28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49,
50]),)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With