pandas 如何通过熊猫数据框获取行增量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30056617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:19:03  来源:igfitidea点击:

How to get row delta value by pandas dataframe

pythonpandas

提问by JustinDoghouse

I got a dataframe combined by with index = datetimeand 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?

我该怎么办?

回答by Nicholas Aldershof

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,则您将需要使用此处记录的 resample 方法重新采样时间序列: http://pandas。 pydata.org/pandas-docs/dev/timeseries.html