I want to apply a weighted sum to a DataFrame. In the past I have used
for sec_id, sec_df_unidx in grouped:
if sec_df_unidx.shape[0] > 3:
pd.rolling_sum(sec_df[added_cols], 4)
I want to apply a weighted average to the sum where the most recent is multiplied by 0.6, 2nd by 0.2, 3rd and 4th by 0.1.
DF:
DATE ID VALUE
2012-12-31 A 100
2013-03-31 A 120
2013-06-30 A 140
2013-09-30 A 160
2013-12-31 A 180
2013-03-31 B 0
2013-06-30 B 5
2013-09-30 B 1
2013-12-31 B 3
2012-12-31 C 45
2013-03-31 C 46
2013-06-30 C 42
2013-09-30 C 30
2013-12-31 C 11
2012-12-31 D 18
2013-03-31 D 9
2013-06-30 D 13
2013-09-30 D 5
2013-12-31 D 11
2012-12-31 E 0
DF with new column:
DATE ID VALUE Weight_Sum
2012-12-31 A 100 NaN
2013-03-31 A 120 NaN
2013-06-30 A 140 NaN
2013-09-30 A 160 146
2013-12-31 A 180 166
2013-03-31 B 0 NaN
2013-06-30 B 5 NaN
2013-09-30 B 1 NaN
2013-12-31 B 3 2.5
2012-12-31 C 45 NaN
2013-03-31 C 46 NaN
2013-06-30 C 42 NaN
2013-09-30 C 30 35.5
2013-12-31 C 11 21.4
2012-12-31 D 18 NaN
2013-03-31 D 9 NaN
2013-06-30 D 13 NaN
2013-09-30 D 5 8.3
2013-12-31 D 11 9.8
2012-12-31 E 0 NaN
Can I do this with a rolling_apply or rolling_sum? Or will I have to do a for loop?
Thank you.