试图在熊猫中解决这个问题

时间:2019-04-11 16:12:54

标签: python-3.x pandas

我在Excel文件中有一些数据。有2列:

  • time_pull:时间戳
  • value_diff:措施之一

我需要基于value_diff列创建第3列(警报与否)。如果连续5行的值> 10,则是否创建警报

time_pull value_diff    alert
3/1/19 0:44 0.110001    No Alert
3/1/19 0:45 1.089996    No Alert
3/1/19 0:46 13.449997   No Alert
3/1/19 0:47 11.779999   No Alert
3/1/19 0:48 10.869995   No Alert
3/1/19 0:49 11.470001   No Alert
3/1/19 0:50 12.000000   Alert
3/1/19 0:51 0.010002    No Alert
3/1/19 0:52 1.080002    No Alert
3/1/19 0:53 0.119995    No Alert
df['alert'] = np.where((df['value_diff'] > 10 
              (df['value_diff'].shift(-1) > 10) & 
              (df['value_diff'].shift(-2) > 10)
              & (df['value_diff'].shift(-3) > 10) & 
              (df['value_diff'].shift(-4) > 10)),"Alert","No Alert")

1 个答案:

答案 0 :(得分:2)

您可以使用rolling加上.min()来完成此操作:

>>> df
            time_pull  value_diff
0 2019-03-01 00:44:00    0.110001
1 2019-03-01 00:45:00    1.089996
2 2019-03-01 00:46:00   13.449997
3 2019-03-01 00:47:00   11.779999
4 2019-03-01 00:48:00   10.869995
5 2019-03-01 00:49:00   11.470001
6 2019-03-01 00:50:00   12.000000
7 2019-03-01 00:51:00    0.010002
8 2019-03-01 00:52:00    1.080002
9 2019-03-01 00:53:00    0.119995

>>> mask = df["value_diff"].rolling(5).min() >= 10
>>> mask.map({True: "Alert", False: "No Alert"})
0    No Alert
1    No Alert
2    No Alert
3    No Alert
4    No Alert
5    No Alert
6       Alert
7    No Alert
8    No Alert
9    No Alert