title: Is there a faster method to do a Pandas groupby cumulative mean?
body: <p>I am trying to create a lookup reference table in Python that calculates the <code>cumulative mean</code> of a Player's previous (by <code>datetime</code>) games scores, grouped by venue. However, for my specific need, a player should have previously played a minimum of 2 times at the relevant Venue for a <code>'Venue Preference'</code> <code>cumulative mean</code> calculation.</p>
  <p><code>df</code> format looks like the following:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>DateTime</th>
  <th>Player</th>
  <th>Venue</th>
  <th>Score</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>2021-09-25 17:15:00</td>
  <td>Tim</td>
  <td>Stadium A</td>
  <td>20</td>
  </tr>
  <tr>
  <td>2021-09-27 10:00:00</td>
  <td>Blake</td>
  <td>Stadium B</td>
  <td>30</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>My existing code that works perfectly, but unfortunately is very slow, is as follows:</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import pandas as pd
  
  VenueSum = pd.DataFrame(df.groupby(['DateTime', 'Player', 'Venue'])['Score'].sum().reset_index(name = 'Sum'))
  VenueSum['Cumulative Sum'] = VenueSum.sort_values('DateTime').groupby(['Player', 'Venue'])['Sum'].cumsum()
  VenueCount = pd.DataFrame(df.groupby(['DateTime', 'Player', 'Venue'])['Score'].count().reset_index(name = 'Count'))
  VenueCount['Cumulative Count'] = VenueCount.sort_values('DateTime').groupby(['Player', 'Venue'])['Count'].cumsum()
  VenueLookup = VenueSum.merge(VenueCount, how = 'outer', on = ['DateTime', 'Player', 'Venue'])
  VenueLookup['Venue Preference'] = np.where(VenueLookup['Cumulative Count'] &gt;= 2, VenueLookup['Cumulative Sum'] / VenueLookup['Cumulative Count'], np.nan)
  VenueLookup = VenueLookup.drop(['Sum', 'Cumulative Sum', 'Count', 'Cumulative Count'], axis = 1)
  </code></pre>
  <p>I am sure there is a way to calculate the <code>cumulative mean</code> in one step without first calculating the <code>cumulative sum</code> and <code>cumulative count</code>, but unfortunately I couldn't get that to work.</p>