gpt4 book ai didi

python - Pandas :一旦一列达到另一列的某个值,如何返回行值?

转载 作者:太空狗 更新时间:2023-10-29 17:13:10 25 4
gpt4 key购买 nike

这是一个数据示例:

enter image description here

目标:
running_bid_max 大于或等于 ask_price_target_good 中的值创建一个新的时间戳列。然后为 running_bid_min 小于或等于 ask_price_target_bad 创建一个单独的时间戳列。

注意:这将在大量数据上执行,需要尽快计算。我希望我不必通过 iterrows()

遍历所有行

running_bid_minrunning_bid_max 是使用 running.min()pd.running.max() 计算的> 从 future 的某个时间范围开始(这个例子使用了 5 分钟的时间线。所以它将是从当前时间开始的最小、最大 5 分钟的运行)

复制下面的数据然后使用df = pd.read_clipboard(sep=',')

   time,bid_price,ask_price,running_bid_max,running_bid_min,ask_price_target_good,ask_price_target_bad
2019-07-24 07:59:44.432034,291.06,291.26,291.4,291.09,291.46,291.06
2019-07-24 07:59:46.393418,291.1,291.33,291.4,291.09,291.53,291.13
2019-07-24 07:59:48.425615,291.1,291.33,291.4,291.09,291.53,291.13
2019-07-24 07:59:50.084206,291.12,291.33,291.4,291.09,291.53,291.13
2019-07-24 07:59:52.326455,291.12,291.33,291.4,291.09,291.53,291.13
2019-07-24 07:59:54.428181,291.12,291.33,291.4,291.09,291.53,291.13
2019-07-24 07:59:58.550378,291.14,291.35,291.4,291.2,291.55,291.15
2019-07-24 08:00:00.837238,291.2,291.35,291.4,291.2,291.55,291.15
2019-07-24 08:00:57.338769,291.4,291.46,291.51,291.4,291.66,291.26
2019-07-24 08:00:59.058198,291.4,291.46,291.96,291.4,291.66,291.26
2019-07-24 08:01:00.802679,291.4,291.46,291.96,291.4,291.66,291.26
2019-07-24 08:01:02.781289,291.4,291.46,291.96,291.45,291.66,291.26
2019-07-24 08:01:04.645144,291.45,291.46,291.96,291.45,291.66,291.26
2019-07-24 08:01:06.491997,291.45,291.46,292.07,291.45,291.66,291.26
2019-07-24 08:01:08.586688,291.45,291.46,292.1,291.45,291.66,291.26

最佳答案

从你的问题:

creating a new timestamp column for when running_bid_max greater than or equal to the value in ask_price_target_good. Then create a separate timestamp column for when running_bid_min is less than or equal to ask_price_target_bad

这个问题看起来微不足道:

df['g'] = np.where(df.running_bid_max.ge(df.ask_price_target_good), df['time'], pd.NaT)

df['l'] = np.where(df.running_bid_min.le(df.ask_price_target_bad), df['time'], pd.NaT)

还是我遗漏了什么?


更新:您可能希望在上述命令之后ffillbfill:

df['g'] = df['g'].bfill()
df['l'] = df['l'].ffill()

输出,例如df['g']:

0    2019-07-24 08:00:59.058198
1 2019-07-24 08:00:59.058198
2 2019-07-24 08:00:59.058198
3 2019-07-24 08:00:59.058198
4 2019-07-24 08:00:59.058198
5 2019-07-24 08:00:59.058198
6 2019-07-24 08:00:59.058198
7 2019-07-24 08:00:59.058198
8 2019-07-24 08:00:59.058198
9 2019-07-24 08:00:59.058198
10 2019-07-24 08:01:00.802679
11 2019-07-24 08:01:02.781289
12 2019-07-24 08:01:04.645144
13 2019-07-24 08:01:06.491997
14 2019-07-24 08:01:08.586688

关于python - Pandas :一旦一列达到另一列的某个值,如何返回行值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57183192/

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