我的数据框如下:
price direction event High_cross movement
-------------------------------------------------------------
0.00225246 down 0 False False
0.00225506 up crossing up False False
0.00225347 up 0 False False
0.00225345 up 0 False False
0.00225613 up 0 True movement
0.00225506 up 0 True True
0.00225345 up 0 False movement
0.00225235 down 0 False False
0.00224500 down crossing down False False
0.00225266 down 0 False False
0.00225246 up crossing up False False
0.00225506 up 0 False False
0.00225506 down crossing down False False
这是一个相对复杂的请求。如果crossing up
列中有event
,请选择具有High_cross
值True
和movement
值movement
作为列的价格,然后进行比较将其移至crossing down
前的价格行。如果第一个价格小于第二个价格,请在新列中使用True
语句进行通知。我不知道如何开始!有什么帮助吗?请让我知道是否需要澄清...谢谢
小的校正,如果在一个movement
和一个crossing up
之间没有crossing down
,则不应进行计算!谢谢
在上面的示例中将是:选择行
price direction event High_cross movement
----------------------------------------------------
0.00225613 up 0 True movement
因为列High_cross
为True
并且movement
的值为movement
,所以从这一行中获取价格0.00225613
并将其与来自排在crossing down
事件上方,因此0.00225235
它应该将结果存储为:
price direction event High_cross movement triggered
----------------------------------------------------------------------
0.00225246 down 0 False False
0.00225506 up crossing up False False
0.00225347 up 0 False False
0.00225345 up 0 False False
0.00225613 up 0 True movement
0.00225506 up 0 True True
0.00225345 up 0 False movement
0.00225235 down 0 False False
0.00224500 down crossing down False False True
0.00225266 down 0 False False
0.00225246 up crossing up False False
0.00225506 up 0 False False
0.00225506 down crossing down False False
答案 0 :(得分:1)
已改进问题更新
import pandas as pd
pd.set_option('precision', 8) # To see all decimals
df = pd.DataFrame({
"price":[0.00225246,0.00225506,0.00225347,0.00225345,0.00225613,0.00225506,0.00225345,0.00225235,0.00224500,0.00225266,0.00225246,0.00225506,0.00225506],
"direction":["down","up","up","up","up","up","up","down","down","down","up","up","down"],
"event": [0,"crossing up",0,0,0,0,0,0,"crossing down",0,"crossing up",0,"crossing down"],
"High_cross": [False,False,False,False,True,True,False,False,False,False,False,False,False],
"movement": [False,False,False,False,"movement",True,"movement",False,False,False,False,False,False]
})
# Add result column
df['triggered'] = "No"
pre_row = []
match_price = None
match_price_2 = None
matched = False
start_search = False
for index,row in df.iterrows():
if index == 0:
pre_row = row
continue
if row["event"] == 'crossing up':
start_search = True
if start_search and row["High_cross"] == True and row["movement"] == 'movement':
match_price = row["price"]
matched = True
if matched and row["event"] == 'crossing down':
match_price_2 = pre_row["price"]
#Only update when condition it's true
if (match_price < match_price_2) == False:
df.at[index, 'triggered'] = result
matched = False
start_search = False
pre_row = row
print(df)
price direction event High_cross movement triggered
------------------------------------------------------------------------
0 0.00225246 down 0 False False No
1 0.00225506 up crossing up False False No
2 0.00225347 up 0 False False No
3 0.00225345 up 0 False False No
4 0.00225613 up 0 True movement No
5 0.00225506 up 0 True True No
6 0.00225345 up 0 False movement No
7 0.00225235 down 0 False False No
8 0.00224500 down crossing down False False Yes
9 0.00225266 down 0 False False No
10 0.00225246 up crossing up False False No
11 0.00225506 up 0 False False No
12 0.00225506 down crossing down False False No