gpt4 book ai didi

python Pandas : Finding derivatives from Dataframe

转载 作者:太空宇宙 更新时间:2023-11-03 14:03:38 27 4
gpt4 key购买 nike

我的数据框看起来像这样:

                     date     time   sym        ask0        ask1        ask2  
0 2017-08-21 07:00:00 07:00:00 7YR 100.812500 100.828125 100.843750
1 2017-08-21 07:00:15 07:00:15 7YR 100.812500 100.828125 100.843750
2 2017-08-21 07:00:30 07:00:30 7YR 100.812500 100.828125 100.843750
3 2017-08-21 07:00:45 07:00:45 7YR 100.812500 100.828125 100.843750
4 2017-08-21 07:01:00 07:01:00 7YR 100.812500 100.828125 100.843750
5 2017-08-21 07:01:15 07:01:15 7YR 100.812500 100.828125 100.843750
6 2017-08-21 07:01:30 07:01:30 7YR 100.812500 100.828125 100.843750
7 2017-08-21 07:01:45 07:01:45 7YR 100.812500 100.828125 100.843750
8 2017-08-21 07:02:00 07:02:00 7YR 100.812500 100.828125 100.843750
9 2017-08-21 07:02:15 07:02:15 7YR 100.812500 100.828125 100.843750
10 2017-08-21 07:02:30 07:02:30 7YR 100.812500 100.828125 100.843750
11 2017-08-21 07:02:45 07:02:45 7YR 100.828125 100.843750 100.859375
12 2017-08-21 07:03:00 07:03:00 7YR 100.812500 100.828125 100.843750
13 2017-08-21 07:03:15 07:03:15 7YR 100.828125 100.843750 100.859375
14 2017-08-21 07:03:30 07:03:30 7YR 100.828125 100.843750 100.859375
15 2017-08-21 07:03:45 07:03:45 7YR 100.828125 100.843750 100.859375
16 2017-08-21 07:04:00 07:04:00 7YR 100.828125 100.843750 100.859375
17 2017-08-21 07:04:15 07:04:15 7YR 100.828125 100.843750 100.859375
18 2017-08-21 07:04:30 07:04:30 7YR 100.828125 100.843750 100.859375
19 2017-08-21 07:04:45 07:04:45 7YR 100.828125 100.843750 100.859375
20 2017-08-21 07:05:00 07:05:00 7YR 100.828125 100.843750 100.859375
21 2017-08-21 07:05:15 07:05:15 7YR 100.828125 100.843750 100.859375
22 2017-08-21 07:05:30 07:05:30 7YR 100.828125 100.843750 100.859375

我想计算导数:d = d(ask0)/dt

其中 dt = 2 分钟。

例如,

t = 2017-08-21 07:04:30 07:04:30    ask@t=100.828125 
t-1 = 2017-08-21 07:04:30 07:02:30 ask@t-1=100.812500
dask/dt = (100.828125-100.812500)/2 = 0.0078125

我知道我可以通过迭代数据框中的每一行来做到这一点。

但我想知道是否有一个 Pandas 函数可以完成这项工作。

最佳答案

第一步是确保 datedatetime

df.date = pd.to_datetime(df.date)

解决方案

df.set_index('date').ask0.rolling('121s').apply(lambda x: x[-1] - x[0]) / 2

date
2017-08-21 07:00:00 0.000000
2017-08-21 07:00:15 0.000000
2017-08-21 07:00:30 0.000000
2017-08-21 07:00:45 0.000000
2017-08-21 07:01:00 0.000000
2017-08-21 07:01:15 0.000000
2017-08-21 07:01:30 0.000000
2017-08-21 07:01:45 0.000000
2017-08-21 07:02:00 0.000000
2017-08-21 07:02:15 0.000000
2017-08-21 07:02:30 0.000000
2017-08-21 07:02:45 0.007812
2017-08-21 07:03:00 0.000000
2017-08-21 07:03:15 0.007812
2017-08-21 07:03:30 0.007812
2017-08-21 07:03:45 0.007812
2017-08-21 07:04:00 0.007812
2017-08-21 07:04:15 0.007812
2017-08-21 07:04:30 0.007812
2017-08-21 07:04:45 0.000000
2017-08-21 07:05:00 0.007812
2017-08-21 07:05:15 0.000000
2017-08-21 07:05:30 0.000000
Name: ask0, dtype: float64

关于 python Pandas : Finding derivatives from Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46105315/

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