【发布时间】:2020-04-02 11:02:53
【问题描述】:
我有这样的数据框:
Price Signal
0 28.68 -1
1 33.36 1
2 44.7 -1
3 43.38 1 ---- smaller than Price[2] # False: Drop row[3,4]
4 41.67 -1
5 42.17 1 ---- smaller than Price[2] # False: Drop row[5,6]
6 44.21 -1
7 46.34 1 ---- greater than Price[2] # True: Keep
8 45.2 -1
9 43.4 1 ---- Still Keep because it is the last row
如果信号 1 的价格高于之前的价格,我的逻辑是保留该行。如果不是,它将丢弃它的行和下一行,因为信号必须散布在 -1 和 1 之间,并且还必须将下一个信号 1 与上面的最后一个信号进行比较(我在上面的数据帧快照中已经解释过)。
最后一个Signal 1仍然保留,虽然它不满足条件,因为规则是Signal列的最后一项必须是1
直到现在我的努力都在这里:
def filter_sell(df):
# For export the result
filtered_sell_df = pd.DataFrame()
for i in range(0, len(df) + 1):
if df.iloc[i]["Signal"] == 1:
if df.iloc[i]["Price"] > df.iloc[i - 1]["Price"]:
pass
else:
try:
df.drop([i, i + 1])
filter_sell(df)
# Try to handle the i + 1 above since len(df) is changed
except RecursionError:
break
else:
pass
我是写递归的新手,谢谢你的帮助!
【问题讨论】:
标签: python-3.x pandas recursion