Hello I have this data set below for 5 years of data. I'm including just a sample.
Date | Europe |
---|---|
April 23 | -0.3% |
April 16 | -0.7% |
April 9 | 1% |
April 2 | 2% |
March 26 | 3% |
I would like to take find the average between two dates. For example the average between April 23 and March 26 should be 1.35%. Its important to note it needs to be between two dates not a rolling avg.
I have tried to average between the two periods similar like a y/y change calculation but it gives me an error. The difference between the rows will be constant 52 rows. So I need to find the average between row 52 and row 1.
Y/Y Change Calculation
df=df.pct_change(periods = -52)
df=df.mean(periods=-52) #Error#
Thank you in advanced for your help.
CodePudding user response:
We can use shift
function to get the value from previous periods
row and then average the previous value with current value.
df = pd.DataFrame({
'value':[1,2,3,4,5,6,7,8,9,10]
})
df['lag_3_value'] = df['value'].shift(periods = 3)
df['avg_between_current_and_lag_3'] = (df['value'] df['lag_3_value']) / 2
Output:
value | lag_3_value | avg_between_current_and_lag_3 |
---|---|---|
1 | nan | nan |
2 | nan | nan |
3 | nan | nan |
4 | 1 | 2.5 |
5 | 2 | 3.5 |
6 | 3 | 4.5 |
7 | 4 | 5.5 |
8 | 5 | 6.5 |
9 | 6 | 7.5 |
10 | 7 | 8.5 |
CodePudding user response:
Maybe not the shortest line possible, But it is working:
np.mean(df.loc[df['date'].isin(['date_one','date_two'])].value.to_numpy())
Where 'value' is the column name of 'europe'
It will work also given any number of dates in the isin() function.