当前位置:  开发笔记 > 编程语言 > 正文

在pandas Series或DataFrame中查找最后一个真值的索引

如何解决《在pandasSeries或DataFrame中查找最后一个真值的索引》经验,为你挑选了2个好方法。

我正在尝试在pandas布尔系列中找到最后一个True值的索引.我目前的代码如下所示.有没有更快或更清洁的方式这样做?

import numpy as np
import pandas as pd
import string

index = np.random.choice(list(string.ascii_lowercase), size=1000)
df = pd.DataFrame(np.random.randn(1000, 2), index=index)
s = pd.Series(np.random.choice([True, False], size=1000), index=index)

last_true_idx_s = s.index[s][-1]
last_true_idx_df = df[s].iloc[-1].name

jezrael.. 11

您可以使用与Andy Hayden的argmaxidxmax相同的答案:

print s[::-1].idxmax()

比较:

这些时间将取决于s的大小以及Trues的数量(和位置) - 谢谢.

In [2]: %timeit s.index[s][-1]
The slowest run took 6.92 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 35 µs per loop

In [3]: %timeit s[::-1].argmax()
The slowest run took 6.67 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 126 µs per loop

In [4]: %timeit s[::-1].idxmax()
The slowest run took 6.55 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 127 µs per loop

In [5]: %timeit s[s==True].last_valid_index()
The slowest run took 8.10 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 261 µs per loop

In [6]: %timeit (s[s==True].index.tolist()[-1])
The slowest run took 6.11 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 239 µs per loop

In [7]: %timeit (s[s==True].index[-1])
The slowest run took 5.75 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 227 µs per loop

编辑:

下一解决方案

print s[s==True].index[-1]

EDIT1:解决方案

(s[s==True].index.tolist()[-1])

被删除的答案.



1> jezrael..:

您可以使用与Andy Hayden的argmaxidxmax相同的答案:

print s[::-1].idxmax()

比较:

这些时间将取决于s的大小以及Trues的数量(和位置) - 谢谢.

In [2]: %timeit s.index[s][-1]
The slowest run took 6.92 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 35 µs per loop

In [3]: %timeit s[::-1].argmax()
The slowest run took 6.67 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 126 µs per loop

In [4]: %timeit s[::-1].idxmax()
The slowest run took 6.55 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 127 µs per loop

In [5]: %timeit s[s==True].last_valid_index()
The slowest run took 8.10 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 261 µs per loop

In [6]: %timeit (s[s==True].index.tolist()[-1])
The slowest run took 6.11 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 239 µs per loop

In [7]: %timeit (s[s==True].index[-1])
The slowest run took 5.75 times longer than the fastest. This could mean that an intermediate result is being cached 
1000 loops, best of 3: 227 µs per loop

编辑:

下一解决方案

print s[s==True].index[-1]

EDIT1:解决方案

(s[s==True].index.tolist()[-1])

被删除的答案.



2> EdChum - Rei..:

用途last_valid_index:

In [9]:
s.tail(10)

Out[9]:
h    False
w     True
h    False
r     True
q    False
b    False
p    False
e    False
q    False
d    False
dtype: bool

In [8]:
s[s==True].last_valid_index()

Out[8]:
'r'

推荐阅读
路人甲
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有