使用循环在数据框的单列中查找多个值

问题描述 投票:0回答:1

我对 python 相当陌生,正在使用具有数百万行的数据框。我有一个值波动的时间序列,需要识别具有特定值的行。具体来说,我正在搜索值超过 50 的每个时间点。我知道我需要创建一个循环来识别当前值 > 50 且前一个值是 <= 50 but I am struggling with the syntax.

的行

Sample Data

此外,我需要获取这些行并构建一个新的数据框。

非常感谢任何指导!

我能够使用下面的代码识别并提取第一个大于 50 的值,但我需要继续循环该列。

test = test_df.sort_values(by=['Time','Value'])
x = test.loc[test['Value'] > 50].iloc[0]
python dataframe loops
1个回答
0
投票

IIUC 你可以使用

pd.Series.shift()
来创建蒙版:

mask = (df["Value"] > 50) & (df["Value"].shift() <= 50)
print(df[mask])

打印:

                   Time  Value
4   2024_03_05_13_25_53     62
11  2024_03_05_13_26_00     67
© www.soinside.com 2019 - 2024. All rights reserved.