JustinDoghouse
JustinDoghouse

Reputation: 53

How to get row delta value by pandas dataframe

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?

Upvotes: 5

Views: 15786

Answers (1)

Nicholas Aldershof
Nicholas Aldershof

Reputation: 265

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

Upvotes: 12

Related Questions