gpt4 book ai didi

python - 需要迭代行以检查条件并在满足条件时从不同列检索值

转载 作者:行者123 更新时间:2023-12-01 01:02:19 25 4
gpt4 key购买 nike

我有股票的每日价格数据。粘贴最后 31 行数据作为示例数据集,如下所示:

       Date         RSI         Smooth          max         min
110 2019-02-13 38.506874 224.006543 NaN NaN
111 2019-02-14 39.567068 227.309923 NaN NaN
112 2019-02-15 43.774479 229.830776 NaN NaN
113 2019-02-18 43.651440 231.690179 NaN NaN
114 2019-02-19 43.467237 232.701976 NaN NaN
115 2019-02-20 44.370123 233.526131 NaN NaN
116 2019-02-21 45.605073 233.834988 233.834988 NaN
117 2019-02-22 46.837518 232.335179 NaN NaN
118 2019-02-25 42.087860 229.570711 NaN NaN
119 2019-02-26 39.008014 226.379526 NaN NaN
120 2019-02-27 39.542339 225.607475 NaN 225.607475
121 2019-02-28 39.051104 228.305615 NaN NaN
122 2019-03-01 48.191687 232.544289 NaN NaN
123 2019-03-05 51.909527 237.063534 NaN NaN
124 2019-03-06 52.988668 240.243201 NaN NaN
125 2019-03-07 54.205990 242.265173 NaN NaN
126 2019-03-08 54.967076 243.912033 NaN NaN
127 2019-03-11 58.080738 244.432163 244.432163 NaN
128 2019-03-12 55.587328 243.573710 NaN NaN
129 2019-03-13 51.714123 241.191933 NaN NaN
130 2019-03-14 48.948075 238.470485 NaN NaN
131 2019-03-15 46.615111 236.144640 NaN NaN
132 2019-03-18 48.219815 233.588265 NaN NaN
133 2019-03-19 41.866898 230.271903 NaN 230.271903
134 2019-03-20 34.818844 239.457110 NaN NaN
135 2019-03-22 42.167870 246.824173 NaN NaN
136 2019-03-25 60.228588 255.294124 NaN NaN
137 2019-03-26 66.896640 267.069173 NaN NaN
138 2019-03-27 68.823285 278.222343 NaN NaN
139 2019-03-28 63.654023 289.042091 289.042091 NaN

我正在尝试开发代码逻辑,如下所示:如果 max > 0,则搜索前一个非零最大值并将其分配给 max2。另外,将先前非零最大值对应的 RSI 指定为 RSI2。

期望的输出:对于数据集中的第 139 行,max2 将为 244.432163,RSI2 将为 58.080738对于数据集中的第 138 行,max2 将为 0,RSI 2 将为 0,依此类推...

我尝试了不同的方法,但没有成功获得任何输出,因此我没有可粘贴的示例代码。

我也尝试过使用 if 循环,但无法使其工作。我对编程很陌生。

最佳答案

首先您需要iterate the dataframe .

然后您将需要存储下一次点击时需要保存的先前值。由于您总是会返回到之前的最大值,因此您可以在循环时重复使用它。

像这样的东西(没有测试,只是为了一个想法):

last_max = 0   
last_rsi = 0
for index, row in df.iterrows():
if row['max']:
row['max2'] = last_max
row['rsi2'] = last_rsi
last_max = row['max'] # store this max/rsi for next time
last_rsi = row['rsi']

关于python - 需要迭代行以检查条件并在满足条件时从不同列检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55692942/

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