title: Groupby, shif and rolling mean in pandas
body: <p>I am trying to groupby, shift and do a rolling mean in pandas. I have searched for solutions but without luck. I have a workaround, but it's not the best. Especially since I need to do rolling standard deviations in future. Could anyone help me and suggest a better way to do this?</p>
  
  <p>Input data:</p>
  
  <pre><code>df = pd.DataFrame({'month': [201912, 202001, 202001, 202002, 202002, 202003, 202003, 202004],
                 'target': [0, 1, 0, 1, 1, 0, 0, 1]
                },
                index = [14, 15, 16, 17, 18, 19, 20, 21])
  </code></pre>
  
  <p>Expected output for a 2-month rolling mean and shift of one month:</p>
  
  <pre><code>df = pd.DataFrame({'month': [201912, 202001, 202002, 202003, 202004],
                 'roll_2m': [np.nan, np.nan, 0.33, 0.75, 0.5]
                },
                index = [1, 2, 3, 4, 5])
  </code></pre>
  
  <p>A bad workaround to solve this is something like:</p>
  
  <pre><code>rolling_count = df.shift(1).target['count'].rolling(2).sum()
  rolling_sum = df.shift(1).target['sum'].rolling(2).sum()
  rolling_mean = rolling_sum/rolling_count                           
  df['roll_2m'] = rolling_mean
  </code></pre>