Using Boolean Indexing¶
In [1]:
import numpy as np
import pandas as pd
s = pd.Series([True, False, True, True, False, False, False, True])
s[s].index
Out[1]:
Use .values
if you need a np.array
object.
In [6]:
s[s].index.values
Out[6]:
Using np.nonzero¶
In [7]:
np.nonzero(s)
Out[7]:
Using np.flatnonzero¶
In [8]:
np.flatnonzero(s)
Out[8]:
Using np.where¶
In [9]:
np.where(s)[0]
Out[9]:
Using np.argwhere¶
In [13]:
np.argwhere(s).ravel()
Out[13]:
Using pd.Series.index¶
In [14]:
s.index[s]
Out[14]:
Using python's built-in filter¶
In [15]:
[*filter(s.get, s.index)]
Out[15]:
Using list comprehension¶
In [17]:
[i for i in s.index if s[i]]
Out[17]:
In [ ]: