'Apply a weighted decay that changes over time in Python

I have a dataframe in Python that looks like the one below:

enter image description here

I want to calculate the dnf_rate_weighted so that there's a 0.95 decay for each stage going back the last 4 stages. The dnf_rate is just the sum of the last four dnf values divided by 4, but what I want in the dnf_rate_weighted is for the below calculation, using stage 6 as an example:

((0*.95)+(1*(0.95^2))+(0*(0.95^3))+(0*(0.95^4)))/4 = 0.226

The other complication is that for a driver's first 1, 2, and 3 stages I want the same but only going back 1, 2, and 3 stages, respectively instead of imputing an NaN value.

This is just a sample of the data, and I will also need to group it by driver_id as well.

Sample code to build the Pandas dataframe:

df = pd.DataFrame({'driver_id': {0: 'Max',
  1: 'Max',
  2: 'Max',
  3: 'Max',
  4: 'Max',
  5: 'Max',
  6: 'Max',
  7: 'Max',
  8: 'Max',
  9: 'Max',
  10: 'Max',
  11: 'Max',
  12: 'Max',
  13: 'Max',
  14: 'Max',
  15: 'Max',
  16: 'Max',
  17: 'Max',
  18: 'Max',
  19: 'Max'},
 'season': {0: 2019,
  1: 2019,
  2: 2019,
  3: 2019,
  4: 2019,
  5: 2019,
  6: 2019,
  7: 2019,
  8: 2019,
  9: 2019,
  10: 2020,
  11: 2020,
  12: 2020,
  13: 2020,
  14: 2020,
  15: 2020,
  16: 2020,
  17: 2020,
  18: 2020,
  19: 2020},
 'stage': {0: 1,
  1: 2,
  2: 3,
  3: 4,
  4: 5,
  5: 6,
  6: 7,
  7: 8,
  8: 9,
  9: 10,
  10: 1,
  11: 2,
  12: 3,
  13: 4,
  14: 5,
  15: 6,
  16: 7,
  17: 8,
  18: 9,
  19: 10},
 'dnf': {0: 0,
  1: 0,
  2: 0,
  3: 0,
  4: 1,
  5: 0,
  6: 0,
  7: 0,
  8: 0,
  9: 0,
  10: 1,
  11: 0,
  12: 1,
  13: 0,
  14: 0,
  15: 0,
  16: 0,
  17: 0,
  18: 0,
  19: 0},
 'career_race': {0: 1,
  1: 2,
  2: 3,
  3: 4,
  4: 5,
  5: 6,
  6: 7,
  7: 8,
  8: 9,
  9: 10,
  10: 11,
  11: 12,
  12: 13,
  13: 14,
  14: 15,
  15: 16,
  16: 17,
  17: 18,
  18: 19,
  19: 20},
 'dnf_rate': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.25,
  5: 0.25,
  6: 0.25,
  7: 0.25,
  8: 0.0,
  9: 0.0,
  10: 0.25,
  11: 0.25,
  12: 0.5,
  13: 0.5,
  14: 0.25,
  15: 0.25,
  16: 0.0,
  17: 0.0,
  18: 0.0,
  19: 0.0},
 'dnf_rate_weighted': {0: 0.0,
  1: 0.0,
  2: 0.0,
  3: 0.0,
  4: 0.2375,
  5: 0.225625,
  6: 0.21434375,
  7: 0.0,
  8: 0.0,
  9: 0.0,
  10: 0.2375,
  11: 0.225625,
  12: 0.45184375,
  13: 0.225625,
  14: 0.21434375,
  15: 0.0,
  16: 0.0,
  17: 0.0,
  18: 0.0,
  19: 0.0}})


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source