[英]Pandas dataframe: Find location where value changes from x to y
I am trying to find the points in a dataframe where the data hits a max value, holds it for a time, then drops down again (See image below). 我试图在数据框中找到数据达到最大值的点,将其保留一段时间,然后再次下降(请参见下图)。
I am attempting to find the index's where the value first hits the max and where it first leaves it. 我正在尝试查找索引值首次达到最大值的位置以及索引值首次离开最大值的位置。 I've attempted it in the following way. 我已经尝试过以下方式。
ent = data.loc[data['ESC_Command'] == 1600 and data['ESC_Command'].shift() < 1600]
lve = data.loc[data['ESC_Command'] == 1600 and data['ESC_Command'].shift(-1) < 1600]
But when I run this I get the following error. 但是,当我运行它时,出现以下错误。
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
If I run either the 'equals to 1600' or the '< 1600' with the shift I get the expected boolean list but adding a logical statement gets me that error. 如果我在移位时运行“等于1600”或“ <1600”,则会得到预期的布尔值列表,但是添加逻辑语句会得到该错误。 Don't suppose anyone can shed some light onto what I'm missing? 难道没有人能对我所缺少的东西有所了解吗?
Thanks in advance! 提前致谢!
You will want to use the bitwise operator ( &
) to combine your masks ( (data['ESC_Command'] == 1600) & (data['ESC_Command'].shift() < 1600)
). 您将需要使用按位运算符( &
)组合掩码( (data['ESC_Command'] == 1600) & (data['ESC_Command'].shift() < 1600)
)。
and
is the logical operator and is unable to compare series, hence the ValueError
. and
是逻辑运算符,无法比较序列,因此ValueError
。
Also, you can use data['ESC_Command'].max()
to dynamically find the maximum value in the column. 另外,您可以使用data['ESC_Command'].max()
在列中动态查找最大值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.