Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get row delta value by pandas dataframe

Tags:

python

pandas

I got a dataframe combined by with index = datetime and 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:

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

What should I do?

like image 974
JustinDoghouse Avatar asked Jan 09 '23 10:01

JustinDoghouse


1 Answers

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

like image 171
Nicholas Aldershof Avatar answered Jan 15 '23 04:01

Nicholas Aldershof