Reputation: 519
I have dataframe of time series and I want to take the % change between a value 3days prior and 1day prior to the current date. I have shown an example below
the dataframe is shown before
import pandas as pd
df = pd.DataFrame({'Date' : ['2014-03-27', '2014-03-28', '2014-03-31', '2014-04-01', '2014-04-02', '2014-04-03', '2014-04-04', '2014-04-07','2014-04-08', '2014-04-09'],
'income': [1849.04, 1857.62, 1872.34, 1885.52, 1890.9, 1888.77, 1865.09, 1845.04, 1851.96, 1872.18],
})
the expected answer, in this case, is as shown below in the table
value for 27/3/2014 is 0.015019 = 1885.52/1857.62-1 for 28/3/2020 is 0.009913 = 1890.9/1872.34-1
and so on. How do I get that?
Upvotes: 0
Views: 52
Reputation: 765
Simple. Use .shift
df['%Change'] = df.income.shift(-3)/df.income.shift(-1)-1
Upvotes: 3