Using 布尔索引 https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#boolean-indexing
>>> s = pd.Series([True, False, True, True, False, False, False, True])
>>> s[s].index
Int64Index([0, 2, 3, 7], dtype='int64')
如果需要一个np.array
对象,得到.values
>>> s[s].index.values
array([0, 2, 3, 7])
Using np.nonzero https://docs.scipy.org/doc/numpy/reference/generated/numpy.nonzero.html
>>> np.nonzero(s)
(array([0, 2, 3, 7]),)
Using np.flatnonzero https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.flatnonzero.html
>>> np.flatnonzero(s)
array([0, 2, 3, 7])
Using np.where https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html
>>> np.where(s)[0]
array([0, 2, 3, 7])
Using np.argwhere https://docs.scipy.org/doc/numpy/reference/generated/numpy.argwhere.html
>>> np.argwhere(s).ravel()
array([0, 2, 3, 7])
Using pd.Series.index https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.index.html#pandas.Series.index
>>> s.index[s]
array([0, 2, 3, 7])
使用Python的内置filter https://docs.python.org/3/library/functions.html#filter
>>> [*filter(s.get, s.index)]
[0, 2, 3, 7]
使用列表理解
>>> [i for i in s.index if s[i]]
[0, 2, 3, 7]