Pandas:根据两个不同的列条件选择值价格 [英] Pandas: select the values price based on two different column conditions

查看:90
本文介绍了Pandas:根据两个不同的列条件选择值价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框如下所示:

 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

这是一个相对复杂的请求.当 event 列中有 crossing up 时,选择具有作为列的 High_crossTrue 的价格和movementmovement 并将其与 向下交叉 之前的价格行进行比较.如果第一个价格小于第二个价格,则在新列中使用 True 语句通知它.我不知道如何开始!有什么帮助吗?如果您需要澄清,请告诉我...谢谢

This is a relative complexe request. When there is a crossing up in the event column, select the price which has as column's High_cross value True and movement value movement and compare it to the price row right before a crossing down. If the first price is smaller than the second price, notify it with a True statement in a new column. I have no idea on how to start! Any help? Please let me know if you need clarifications... thanks

小更正,如果在一次向上交叉和一次向下交叉之间没有移动,则不应进行计算!谢谢

Small correction, if there is no movement between one crossing up and a crossing down, the computation should not be done! thanks

在上面的例子中它将是:选择行

In the example above it will be: select the line

price       direction   event   High_cross  movement
----------------------------------------------------
0.00225613  up          0       True        movement 

因为 High_cross 列是 True 并且 movement 有值 movement,所以从这一行取价格 0.00225613 并将其与 crossing down 事件正上方一行的价格进行比较,所以 0.00225235

because the column High_cross is True and movement has value movement, from this line take the price 0.00225613 and compares it with the price from a row right above a crossing down event so 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

推荐答案

UPDATE

随着问题更新而改进

UPDATE

Improved with question update

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

这篇关于Pandas:根据两个不同的列条件选择值价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆