title: group by rolling sum in pandas
body: <p>I've been working on this particular problem and have tried really all sorts of different methods but can't seem to solve it.    I have a dataframe that looks like the following:</p>
  
  <pre><code>    Sequence GameID PlayerA PlayerB Left_Right
     0    1     101    334    326      Right
     1    2     101    334    326      Right
     2    3     101    334    326      Left
     3    4     101    326    334      Left
     4    5     101    326    334      Right
     5    6     101    326    334      Left
     6    7     101    326    334      Right
     7    8     101    326    334      Right
     8    9     101    326    334      Left
     9    10    101    334    326      Left
  </code></pre>
  
  <p>What I essentially am going to ultimately created is two columns, one with a rolling sum number of 'Right' and another column of a rolling sum of number of 'Left'.  It is to be grouped by GameID and also PlayerA.  In the dataframe above I only show part of one gameid, as it is a snippit.  This is similar to a countif in excel where my if condition would be 'Right'. </p>
  
  <p>My understanding in pandas is to use a pd.rolling_sum() function but i'm not quite sure how to groupby and apply it while setting a condition.  I've also tried using cumcount() to no avail</p>
  
  <pre><code>df.groupby(['GameID','PlayerA']).Left_Right.cumcount()
  </code></pre>
  
  <p>Ultimately one of the new columns would look like this.  I'm not sure where I would specify 'Right' in the function above.  When I did a pd.rolling_sum I used a shift, but again I couldn't find where to groupby with this.</p>
  
  <pre><code>pd.rolling_sum((df["Left_Right"] == "Right"), 10000000, min_periods=0).shift(1)
  
  
      Sequence GameID PlayerA PlayerB Left_Right  CountIf_Right
     0    1     101    334    326      Right          0  
     1    2     101    334    326      Right          1
     2    3     101    334    326      Left           2
     3    4     101    326    334      Left           0
     4    5     101    326    334      Right          0
     5    6     101    326    334      Left           1
     6    7     101    326    334      Right          1
     7    8     101    326    334      Right          2
     8    9     101    326    334      Left           3
     9    10    101    334    326      Left           2
  </code></pre>
  
  <p>So to go through that column, it is grouped by gameID and PlayerA and is a rolling sum / countif with the condition being 'Right'.   So in row 1, 0 rights have occured in rows above it.  In row 2, 1 has occured above it for the player in the game.</p>
  
  <p>Any help would be greatly appreciated</p>