gpt4 book ai didi

python - 比较 Pandas 中的当前行和上一行

转载 作者:行者123 更新时间:2023-12-05 08:54:13 67 4
gpt4 key购买 nike

我有一个股票数据框:

   Open     High      Low    Close  Volume        rb          us  \
0 1.20821 1.20821 1.20793 1.20794 138.96 0.022347 100.000000
1 1.20794 1.20795 1.20787 1.20788 119.61 0.004967 85.714286
2 1.20788 1.20793 1.20770 1.20779 210.42 0.007451 64.285714
3 1.20779 1.20791 1.20779 1.20789 77.51 0.008280 83.333333
4 1.20789 1.20795 1.20789 1.20792 56.97 0.002484 50.000000

ls color
0 96.428571 black
1 85.714286 black
2 50.000000 black
3 100.000000 white
4 100.000000 white

我想比较当前行数据和前一行数据,像这样:

if(df['color'] == df['color'].shift(-1)):
if((df['Close'] >= df['Open'].shift(-1) and df['Open']>=df['Close'].shift(-1)):
df['Position'] = UP
if((df['Close'] < df['Open'].shift(-1) and df['Open']<=df['Close'].shift(-1)):
df['Position'] = DOWN

还有更多的 IF 条件......

无法比较数据

np.where(condition,TRUE,FALSE)

因为我的算法有很多条件。这只是其中的一部分。

如何检查这些条件?

最佳答案

你需要:

df['Position'] = np.where((df['Volume'] > df['Volume'].shift(-1)) & ((df['Close'] >= df['Close'].shift(-1)) & (df['Open'] <= df['Open'])),"UP","DOWN")

输出:

        Open    High    Low Close   Volume  Position
0 1.20821 1.20821 1.20793 1.20794 138.96 UP
1 1.20794 1.20795 1.20787 1.20788 119.61 DOWN
2 1.20788 1.20793 1.20770 1.20779 210.42 DOWN
3 1.20779 1.20791 1.20779 1.20789 77.51 DOWN
4 1.20789 1.20795 1.20789 1.20792 56.97 DOWN

关于python - 比较 Pandas 中的当前行和上一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50767799/

67 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com