简体   繁体   English

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

[英]Find index of last true value in pandas Series or DataFrame

I'm trying to find the index of the last True value in a pandas boolean Series. 我正在尝试在pandas布尔系列中找到最后一个True值的索引。 My current code looks something like the below. 我目前的代码如下所示。 Is there a faster or cleaner way of doing this? 有没有更快或更清洁的方式这样做?

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

You can use idxmax what is the same as argmax of Andy Hayden answer : 您可以使用idxmax与Andy Hayden的argmax相同的答案

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

Comparing: 比较:

These timings are going to be very dependent on the size of s as well as the number (and position) of Trues - thanks. 这些时间将取决于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

EDIT: 编辑:

Next solution: 下一解决方案

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

EDIT1: Solution EDIT1:解决方案

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

was in deleted answer. 被删除的答案。

Use last_valid_index : 使用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'

argmax gets the first True. argmax获得第一个True。 Use argmax on the reversed Series: 反向系列上使用argmax:

In [11]: s[::-1].argmax()
Out[11]: 'e'

Here: 这里:

In [12]: s.tail()
Out[12]:
n     True
e     True
k    False
d    False
l    False
dtype: bool

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有简单的方法可以找到熊猫系列中最后一个连续的True值? - Is there a simple way to find the last consecutive True value in a Pandas series? 如何在 Pandas 系列中找到与特定值匹配的最后一次出现索引? - How to find last occurence index matching a certain value in a Pandas Series? 通过与另一个系列进行比较来查找数据帧的索引值 - Find index value of a dataframe by comparing with another series 在Pandas数据帧的行中查找第一个真值 - Find first true value in a row of Pandas dataframe 使用 DateTime 索引在 Pandas DataFrame 中查找每天第一次和最后一次出现值的索引位置 - Find index location of first and last occurrence of a value per day in a Pandas DataFrame with a DateTime index 检查行中的 TRUE、FALSE 值,在另一个数据框中找到匹配的索引,然后对熊猫值求和 - Check TRUE, FALSE value in row, find matching index in another dataframe then sum values pandas 从多索引熊猫数据框中引用熊猫系列值 - Refer to a pandas series value from a multi-index pandas dataframe 在熊猫系列中查找最后一个非零元素的索引 - Find last non-zero element's index in pandas series 将系列索引中的值添加到Pandas DataFrame中等值的行 - Add value from series index to row of equal value in Pandas DataFrame 使用 pandas 和 plotly 访问时间序列 dataframe 中的最后一个值 - Accessing last value in a time series dataframe with pandas and plotly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM