熊猫滚动最接近的值

问题描述 投票:0回答:1

假设我们有以下数据框:

              timestamp      open      high       low     close  delta  atr  last_index  bearish bullish_turning_point
2   04-10-2024 01:54:44  18370.00  18377.75  18367.50  18376.00     32    0        1949    False                  True
5   04-10-2024 03:21:14  18376.50  18383.00  18375.25  18381.25     28    0        3899    False                  True
7   04-10-2024 04:38:54  18378.50  18386.25  18378.25  18385.50    133    0        5199    False                  True
9   04-10-2024 05:30:27  18384.00  18389.50  18378.75  18388.25    135    0        6499    False                  True
12  04-10-2024 06:06:12  18371.00  18378.00  18369.50  18378.00    130    0        8449    False                  True
14  04-10-2024 06:33:44  18372.25  18383.75  18372.00  18376.25     67    0        9749    False                  True
18  04-10-2024 07:21:14  18377.50  18387.75  18376.25  18380.00      8    0       12349    False                  True
22  04-10-2024 07:47:58  18388.00  18396.75  18385.25  18389.50    -30    0       14949    False                  True
25  04-10-2024 08:06:17  18390.75  18397.00  18387.50  18392.00    -25    0       16899    False                  True
28  04-10-2024 08:33:32  18384.75  18398.00  18383.25  18394.00     89    0       18849    False                  True
30  04-10-2024 08:54:35  18391.25  18403.00  18387.75  18399.25     84    0       20149    False                  True
34  04-10-2024 09:11:15  18388.75  18396.25  18385.75  18392.25     15    0       22749    False                  True
43  04-10-2024 10:02:22  18343.50  18350.50  18341.25  18350.50    113    0       28599    False                  True
46  04-10-2024 10:14:44  18352.00  18361.75  18352.00  18360.00    -42    0       30549    False                  True
49  04-10-2024 10:35:49  18354.00  18361.25  18347.75  18358.00     49    0       32499    False                  True
52  04-10-2024 10:54:18  18362.25  18372.00  18361.50  18372.00    180    0       34449    False                  True
56  04-10-2024 11:12:32  18369.25  18379.50  18367.00  18376.50     78    0       37049    False                  True
59  04-10-2024 11:27:27  18370.00  18376.50  18367.50  18373.25     54    0       38999    False                  True
65  04-10-2024 12:01:53  18377.75  18388.25  18377.50  18383.25    108    0       42899    False                  True
73  04-10-2024 12:25:04  18382.00  18386.25  18381.00  18384.75     65    0       48099    False                  True

如何找到每行之前距离“open”最近的“最近”?例如

对于第 30 行 (

close
: 18399.25),这将是第 25 行 (
open
: 18390.75)。对于第 52 行 (
close
: 18372.00),这将是 14 (
open
: 18372.25) 等等。

python pandas
1个回答
0
投票

尝试:

def find_closest_value(idx):
    g = df.loc[: idx.iat[-1]]
    close_ = g.loc[idx.iat[-1], "close"]
    closest_open_to_close = (g.loc[: idx.iat[-2], "open"] - close_).abs().argmin()
    return g.index[closest_open_to_close]


df["nearest"] = df.index.to_frame().expanding(min_periods=2).apply(find_closest_value)
print(df)

打印:

              timestamp      open      high       low     close  delta  atr  last_index  bearish  bullish_turning_point  nearest
2   04-10-2024 01:54:44  18370.00  18377.75  18367.50  18376.00     32    0        1949    False                   True      NaN
5   04-10-2024 03:21:14  18376.50  18383.00  18375.25  18381.25     28    0        3899    False                   True      2.0
7   04-10-2024 04:38:54  18378.50  18386.25  18378.25  18385.50    133    0        5199    False                   True      5.0
9   04-10-2024 05:30:27  18384.00  18389.50  18378.75  18388.25    135    0        6499    False                   True      7.0
12  04-10-2024 06:06:12  18371.00  18378.00  18369.50  18378.00    130    0        8449    False                   True      7.0
14  04-10-2024 06:33:44  18372.25  18383.75  18372.00  18376.25     67    0        9749    False                   True      5.0
18  04-10-2024 07:21:14  18377.50  18387.75  18376.25  18380.00      8    0       12349    False                   True      7.0
22  04-10-2024 07:47:58  18388.00  18396.75  18385.25  18389.50    -30    0       14949    False                   True      9.0
25  04-10-2024 08:06:17  18390.75  18397.00  18387.50  18392.00    -25    0       16899    False                   True     22.0
28  04-10-2024 08:33:32  18384.75  18398.00  18383.25  18394.00     89    0       18849    False                   True     25.0
30  04-10-2024 08:54:35  18391.25  18403.00  18387.75  18399.25     84    0       20149    False                   True     25.0
34  04-10-2024 09:11:15  18388.75  18396.25  18385.75  18392.25     15    0       22749    False                   True     30.0
43  04-10-2024 10:02:22  18343.50  18350.50  18341.25  18350.50    113    0       28599    False                   True      2.0
46  04-10-2024 10:14:44  18352.00  18361.75  18352.00  18360.00    -42    0       30549    False                   True      2.0
49  04-10-2024 10:35:49  18354.00  18361.25  18347.75  18358.00     49    0       32499    False                   True     46.0
52  04-10-2024 10:54:18  18362.25  18372.00  18361.50  18372.00    180    0       34449    False                   True     14.0
56  04-10-2024 11:12:32  18369.25  18379.50  18367.00  18376.50     78    0       37049    False                   True      5.0
59  04-10-2024 11:27:27  18370.00  18376.50  18367.50  18373.25     54    0       38999    False                   True     14.0
65  04-10-2024 12:01:53  18377.75  18388.25  18377.50  18383.25    108    0       42899    False                   True      9.0
73  04-10-2024 12:25:04  18382.00  18386.25  18381.00  18384.75     65    0       48099    False                   True     28.0
© www.soinside.com 2019 - 2024. All rights reserved.