如何通过pandas dataframe获取行增量值

时间:2021-04-27 19:36:40

I got a dataframe combined by with index = datetime and column = {'currentprice'}

我得到了一个数据框,其中包含index = datetime和column = {'currentprice'}

index               currentPrice
2015-03-26 10:09:01 75.75
2015-03-26 10:11:57 75.70

Now I want to get the delta value during every 3 minutes(as an example), the I can get a dataframe like this:

现在我想在每3分钟获得delta值(作为示例),我可以获得这样的数据帧:

index               delta
2015-03-26 10:09:01 -0.05
2015-03-26 10:11:57 0.10
...

What should I do?

我该怎么办?

1 个解决方案

#1


To expand upon the answer given in the comments, you can use

要扩展评论中给出的答案,您可以使用

df['delta'] = df.currentPrice.diff().shift(-1)

to get the difference between the price in one row and the next. However if you're actually interested in finding the difference between time periods separated by 3 minutes, not the 2m56s in your data, you're going to need to resample your timeseries using the resample method as documented here: http://pandas.pydata.org/pandas-docs/dev/timeseries.html

获得一行和下一行的价格差异。但是,如果您真的想要找到相隔3分钟的时间段之间的差异,而不是数据中的2m56s,那么您将需要使用此处记录的重新采样方法重新采样您的时间序列:http:// pandas。 pydata.org/pandas-docs/dev/timeseries.html

#1


To expand upon the answer given in the comments, you can use

要扩展评论中给出的答案,您可以使用

df['delta'] = df.currentPrice.diff().shift(-1)

to get the difference between the price in one row and the next. However if you're actually interested in finding the difference between time periods separated by 3 minutes, not the 2m56s in your data, you're going to need to resample your timeseries using the resample method as documented here: http://pandas.pydata.org/pandas-docs/dev/timeseries.html

获得一行和下一行的价格差异。但是,如果您真的想要找到相隔3分钟的时间段之间的差异,而不是数据中的2m56s,那么您将需要使用此处记录的重新采样方法重新采样您的时间序列:http:// pandas。 pydata.org/pandas-docs/dev/timeseries.html