python布尔系列_python – 获取pandas布尔系列为True的索引列表
生活随笔
收集整理的這篇文章主要介紹了
python布尔系列_python – 获取pandas布尔系列为True的索引列表
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用
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對(duì)象,請(qǐng)獲取.values
>>> s[s].index.values
array([0, 2, 3, 7])
>>> np.nonzero(s)
(array([0, 2, 3, 7]),)
>>> np.flatnonzero(s)
array([0, 2, 3, 7])
>>> np.where(s)[0]
array([0, 2, 3, 7])
>>> np.argwhere(s).ravel()
array([0, 2, 3, 7])
>>> s.index[s]
array([0, 2, 3, 7])
使用python的內(nèi)置filter
>>> [*filter(s.get, s.index)]
[0, 2, 3, 7]
使用列表理解
>>> [i for i in s.index if s[I]]
[0, 2, 3, 7]
總結(jié)
以上是生活随笔為你收集整理的python布尔系列_python – 获取pandas布尔系列为True的索引列表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: js map 排序_数组方法写给女友的一
- 下一篇: setwindowshookex回调函数