如何在if语句中使用Dataframe中的先前值

时间:2018-02-09 13:50:19

标签: python pandas dataframe

我有DataFrame df

df=pd.DataFrame([[47,55,47,50], [33,37,30,25],[61,65,54,57],[25,26,21,22], [25,29,23,28]], columns=['open','high','low','close'])

print(df)

    open    high    low     close
0   47      55      47      50
1   33      37      30      25
2   61      65      54      57
3   25      26      21      22
4   25      29      23      20

我想在if语句中使用以前的值进行比较。 逻辑如下:

 if (close[i-1]/close[i] > 2) and (high[i] < low[i-1]) and 
((open[i] > high[i-1]) or (open[i] <low[i-1])) :

我写了一段代码:

for idx,i in df.iterrows():
    if idx !=0 :
        if ((prv_close/i['close'])>2) and (i['high'] < prv_low) and ((i['open'] > prv_high) or (i['open'] < prv_low)):
            print("Successful")
        else: 
            print("Not Successful")

    prv_close = i['close']
    prv_open = i['open']
    prv_low = i['low']
    prv_high = i['high']

输出:

Not Successful
Not Successful
Successful
Not Successful

但是对于数百万行而言,它需要花费太多时间。还有其他方法可以更快地实现它吗?

P.S:if语句对数据采取的措施有所不同。为简单起见,我使用的是print语句。我的列可能没有相同的顺序,这就是我使用iterrows()而不是itertuples()的原因。

欢迎任何建议。 感谢。

1 个答案:

答案 0 :(得分:1)

d0 = df.shift()

cond0 = (d0.close / df.close) > 2
cond1 = df.high < d0.low
cond2 = df.open > d0.high
cond3 = df.open < d0.low

mask = cond0 & cond1 & (cond2 | cond3)

mask

0    False
1    False
2    False
3     True
4    False
dtype: bool