I have a pandas series with boolean entries. I would like to get a list of indices where the values are True
.
For example the input pd.Series([True, False, True, True, False, False, False, True])
should yield the output [0,2,3,7]
.
I can do it with a list comprehension, but is there something cleaner or faster?
Also works:
s.where(lambda x: x).dropna().index
, and it has the advantage of being easy to chain pipe - if your series is being computed on the fly, you don't need to assign it to a variable.Note that if
s
is computed fromr
:s = cond(r)
than you can also use:r.where(lambda x: cond(x)).dropna().index
.