Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Get Index of True Values of a Sequence in Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Using Boolean Indexing

Int64Index([0, 2, 3, 7], dtype='int64')

Use .values if you need a np.array object.

array([0, 2, 3, 7])

Using np.nonzero

(array([0, 2, 3, 7]),)

Using np.flatnonzero

array([0, 2, 3, 7])

Using np.where

array([0, 2, 3, 7])

Using np.argwhere

array([0, 2, 3, 7])

Using pd.Series.index

Int64Index([0, 2, 3, 7], dtype='int64')

Using python’s built-in filter

[0, 2, 3, 7]

Using list comprehension

[0, 2, 3, 7]