选择 Pandas dataframe 索引,之后一列中的数据都高于特定值

Choose Pandas dataframe index after which data in a column is all higher than a specific value

我在 pandas 中有一个数据框,类似于:

df.head()
                             P1'S1       P1'S2       P1'S3       P1'S4  
Year_Day_Hour_Min_Sec.                                                   
2005-01-20 00:01:00      10.292887    5.849372    5.154812    5.824268   
2005-01-20 00:02:00     423.334728  415.878661  346.619247  333.317992   
2005-01-20 00:03:00     340.753138  429.447699  370.945607  417.832636   
2005-01-20 00:04:00     494.067643  426.577406  332.811715  361.725941   
2005-01-20 00:05:00     415.266039  396.711994  370.289749  398.025802

并且我想获取索引,此时 从该索引开始的所有值 都高于 P1'S1 列中的 400。所以,在这种情况下,正确的答案是索引 2005-01-20 00:04:00

执行此操作的有效方法是什么?最好是可以与任何一种比较条件一起使用的。

这有点间接,但如果我们采用反转列的累积最小值,我们就会知道在该点或之后看到的最低值。 that 的第一个值 > 400 是您要查找的位置:

>>> ((df["P1'S1"].iloc[::-1].cummin().iloc[::-1]) > 400).idxmax()
'2005-01-20 00:04:00'

基本上,一旦我们到达

>>> df["P1'S1"].iloc[::-1].cummin().iloc[::-1]
Year_Day_Hour_Min_Sec.
2005-01-20 00:01:00     10.292887
2005-01-20 00:02:00    340.753138
2005-01-20 00:03:00    340.753138
2005-01-20 00:04:00    415.266039
2005-01-20 00:05:00    415.266039
Name: P1'S1, dtype: float64

我们可以利用idxmax returns第一个达到最大值的索引转

>>> (df["P1'S1"].iloc[::-1].cummin().iloc[::-1] > 400)
Year_Day_Hour_Min_Sec.
2005-01-20 00:01:00    False
2005-01-20 00:02:00    False
2005-01-20 00:03:00    False
2005-01-20 00:04:00     True
2005-01-20 00:05:00     True
Name: P1'S1, dtype: bool

进入2005-01-20 00:04:00.

请注意,我假设我们这里有唯一索引。我们可以解决这个问题,但我还没喝咖啡。 :-)