title: Python Pandas: Calculate cumulative sum using rolling lookback window
body: <p>I am calculating the value for the <em>Total Wins (2 Days)</em> column in the (table below) - also see below for comma separated values.</p>
  
  <p><a href="https://i.stack.imgur.com/riZFF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/riZFF.png" alt="enter image description here"></a></p>
  
  <p>Total Wins (2 Days) is a cumulative count of the number of races the athlete has won either on that given day (eg. Day 5) or the day before (eg. Day 4) - as such, it is a count of the number of wins within a 2 day look back window. (I may want to change the look back window to any number).</p>
  
  <p>For example, on Day 7: Jane gets a count of 1 because she won on Day 7 but lost on Day 6; Bill gets a count of 1 because he lost on Day 7 but won on Day 6. Steve didn't win on either day.</p>
  
  <p>On Day 6, Bill gets a count of 1 because he won on day Day but lost on Day 5. Steve gets a count of 1 because he lost on Day 6 but won on Day 5. Jane won on neither day.</p>
  
  <p>What is the best way to calculate 'Total Wins (2 Days)' in Python?</p>
  
  <p><strong><em>Follow-up Question</em></strong></p>
  
  <p>Also, as a follow-up question: With regards to the param for '.rolling(2)' (ie. 2 in this case), how would I set the param to be a value that is derived from other columns in the table?</p>
  
  <p>What I want to do is change Race Day to a datetime object (see updated table below) and calculate 'Total Wins (X Days)' as the number of races won over the last week, month, year and so on. (The database I am working with is bigger than the example above).</p>
  
  <p>I would prefer to define Year as a calendar year (ie. races won between 2014-01-01 and 2015-01-01), rather than simply 265 days.</p>
  
  <p><a href="https://i.stack.imgur.com/lE6Ud.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lE6Ud.png" alt="enter image description here"></a></p>
  
  <pre><code>Race Day,Athlete,Position,Total Wins,Total Wins (2 Days)
  1,Steve,1,1,1
  1,Jane,2,0,0
  1,Bill,3,0,0
  2,Bill,1,1,1
  2,Steve,2,1,1
  2,Jane,3,0,0
  3,Jane,1,1,1
  3,Bill,2,1,1
  3,Steve,3,1,0
  4,Steve,1,2,1
  4,Jane,2,1,1
  4,Bill,3,1,0
  5,Steve,1,3,2
  5,Jane,2,1,0
  5,Bill,3,1,0
  6,Bill,1,2,1
  6,Steve,2,3,1
  6,Jane,3,1,0
  7,Jane,1,2,1
  7,Bill,2,2,1
  7,Steve,3,3,0
  
  Race Day,Athlete,Position,Total Wins,Total Wins (2 Months)
  2000-01-01,Steve,1,1,?
  2000-01-01,Jane,2,0,?
  2000-01-01,Bill,3,0,?
  2000-01-02,Bill,1,1,?
  2000-01-02,Steve,2,1,?
  2000-01-02,Jane,3,0,?
  2000-01-03,Jane,1,1,?
  2000-01-03,Bill,2,1,?
  2000-01-03,Steve,3,1,?
  2000-01-04,Steve,1,2,?
  2000-01-04,Jane,2,1,?
  2000-01-04,Bill,3,1,?
  2000-01-05,Steve,1,3,?
  2000-01-05,Jane,2,1,?
  2000-01-05,Bill,3,1,?
  2000-01-06,Bill,1,2,?
  2000-01-06,Steve,2,3,?
  2000-01-06,Jane,3,1,?
  2000-01-07,Jane,1,2,?
  2000-01-07,Bill,2,2,?
  2000-01-07,Steve,3,3,?
  </code></pre>