Pandas dataframe:查找值从x变为y的位置

2024-10-03 23:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一个数据帧中找到数据达到最大值的点,保持一段时间,然后再次下降(见下图)。在

enter image description here

我试图找到索引的值第一次到达最大值的位置以及它第一次离开它的位置。我试过以下几种方法。在

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]

但是当我运行这个程序时,我得到了以下错误。在

^{pr2}$

如果我使用shift运行'equals to 1600'或'<;1600',我得到预期的布尔值列表,但是添加一个逻辑语句会得到错误。别以为有人能帮我弄明白我遗漏了什么?在

提前谢谢!在


Tags: andto数据方法程序datashift错误
1条回答
网友
1楼 · 发布于 2024-10-03 23:24:05

您需要使用位运算符(&)组合掩码((data['ESC_Command'] == 1600) & (data['ESC_Command'].shift() < 1600))。在

and是逻辑运算符,无法比较序列,因此ValueError。在


此外,还可以使用data['ESC_Command'].max()动态查找列中的最大值。在

相关问题 更多 >