title: Apply a weighted decay that changes over time in Python
body: <p>I have a dataframe in Python that looks like the one below:</p>
  <p><a href="https://i.stack.imgur.com/igjPO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/igjPO.png" alt="enter image description here" /></a></p>
  <p>I want to calculate the <code>dnf_rate_weighted</code> so that there's a 0.95 decay for each stage going back the last 4 stages. The <code>dnf_rate</code> is just the sum of the last four <code>dnf</code> values divided by 4, but what I want in the <code>dnf_rate_weighted</code> is for the below calculation, using stage 6 as an example:</p>
  <pre><code>((0*.95)+(1*(0.95^2))+(0*(0.95^3))+(0*(0.95^4)))/4 = 0.226
  </code></pre>
  <p>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 <code>NaN</code> value.</p>
  <p>This is just a sample of the data, and I will also need to group it by <code>driver_id</code> as well.</p>
  <p>Sample code to build the Pandas dataframe:</p>
  <pre><code>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}})
  </code></pre>