title: Python - Pandas, Rolling Mean, and difficult Groupby common ID with a few select parameters
body: <p>Hello I am having trouble creating the desired output from a rolling mean and groupby function in python.  Below is a snapshot of my dataframe....</p>
  
  <pre><code>    var_x = range(0, 12)
      var_y = range(20, 44, 2)
      var_z = range(0, 60, 5) 
      ID = ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'c', 'c', 'c']
      df = DataFrame(zip(ID, var_x, var_y, var_z), columns = ['id', 'var_x', 'var_y', 'var_z'])
  
      df
      id   var_x   var_y   var_z
      a   0  20   0
      a   1  22   5
      a   2  24  10
      a   3  26  15
      b   4  28  20
      b   5  30  25
      b   6  32  30
      b   7  34  35
      b   8  36  40
      c   9  38  45
      c  10  40  50
      c  11  42  55
  </code></pre>
  
  <p>With this data I am trying to accomplish 3 things:
  - First I want to calculate the rolling mean using groupby for the 'id' field.  I am able to do this for an individual column but not for, lets say every numerical field (var_y, var_z), using the following code.</p>
  
  <pre><code>    df.groupby('id')['var_x'].apply(pd.rolling_mean, 3) 
  </code></pre>
  
  <ul>
  <li><p>Second,  I would like the average of the previous 3 IDs to populate the current ID, without the values of the current ID included in the current rolling mean iteration (maybe with shift?).  For example, an output that looks like this...</p>
  
  <pre><code>0    NaN    NaN    NaN
  1    NaN    NaN    NaN
  2    NaN    NaN    NaN
  3      1    22     5
  4    NaN    NaN    NaN
  5    NaN    NaN    NaN
  6    NaN    NaN    NaN 
  7      5    30     25
  8      6    32     30
  9     NaN   NaN    NaN
  10    NaN   NaN    NaN
  11    NaN   NaN    NaN
  </code></pre></li>
  </ul>
  
  <p>Notice how the 4th 'a', is the average of the first 3 a's.  The same goes for the 4th and 5th b (taking on the values of the previous 3 averages while excluding its own values).</p>
  
  <ul>
  <li><p>Third, for the IDs that end up with 'NaN' (the first three for every new ID) I would like to iteratively reduce the window from 3 down to 1 (if possible) so that the only values with NaN will be the first instance for each ID.  Output desired will be...</p>
  
  <pre><code>0    NaN    NaN    NaN
  1      0    20     0
  2    0.5    21     2.5
  3      1    22     5
  4    NaN    NaN    NaN
  5      4    28     20
  6    4.5    29     22.5 
  7      5    30     25
  8      6    32     30
  9     NaN   NaN    NaN
  10    9     38     45
  11    9.5   39     47.5       
  </code></pre></li>
  </ul>
  
  <p>Notice how the 3rd a is the average of the first 2 a's.  the second a is the values from the first etc...</p>
  
  <p>Any help would be appreciated.</p>