1 回答
TA贡献1906条经验 获得超3个赞
如果我正确理解了这个问题,那么您想要的是(df.Streak >= 3) & (df.Streak.shift(-1) < 0).
稍微更改您自己示例中的第一行以确保满足这两个条件,这给出了以下内容:
In [15]: df
Out[15]:
Security Difference Buy/Sell Streak Price
Date
2019-02-25 2330 500.0 1 3 238.0
2019-02-26 2330 400.0 1 2 239.0
2019-02-27 2330 200.0 1 3 239.5
2019-03-05 2330 -600.0 -1 -1 233.0
2019-03-06 2330 190.0 1 1 234.0
In [16]: (df.Streak >= 3) & (df.Streak.shift(-1) < 0)
Out[16]:
Date
2019-02-25 False
2019-02-26 False
2019-02-27 True
2019-03-05 False
2019-03-06 False
Name: Streak, dtype: bool
要回答编辑过的问题,您可以使用此掩码获取相关输出:
mask = (df.Streak >= 3) & (df.Streak.shift(-1) < 0)
df['Output1'] = df[mask].Price
df['Output2'] = df[mask].Price / df.shift(-2)[mask].Price
结果如下:
In [139]: df
Out[139]:
Security Difference Buy/Sell Streak Price Output1 Output2
Date
2019-02-25 2330 500.0 1 3 238.0 NaN NaN
2019-02-26 2330 400.0 1 2 239.0 NaN NaN
2019-02-27 2330 200.0 1 3 239.5 239.5 1.023504
2019-03-05 2330 -600.0 -1 -1 233.0 NaN NaN
2019-03-06 2330 190.0 1 1 234.0 NaN NaN
添加回答
举报