python布尔系列_python – 获取pandas布尔系列为True的索引列表
生活随笔
收集整理的這篇文章主要介紹了
python布尔系列_python – 获取pandas布尔系列为True的索引列表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用
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])
>>> 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的內置filter
>>> [*filter(s.get, s.index)]
[0, 2, 3, 7]
使用列表理解
>>> [i for i in s.index if s[I]]
[0, 2, 3, 7]
總結
以上是生活随笔為你收集整理的python布尔系列_python – 获取pandas布尔系列为True的索引列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js map 排序_数组方法写给女友的一
- 下一篇: websocket python爬虫_p