title: Pandas: apply returns list
body: <p>My dataframe is as follows:</p>
  <pre><code>player_id  season_id   game_id  points  mean_to_date  
  200      21999  29900007         10             0     
  200      21999  29900023         20             0     
  200      21200  29900042         10             0     
  200      21200  29900059         20             0     
  200      21200  29900081         30             0     
  300      21999  29900089         10             0     
  300      22111  29900108         10             0     
  300      22111  29900118         20             0     
  300      22111  29900143         30             0
  </code></pre>
  <p>I split it into groups with:</p>
  <pre><code>grouped = frame.groupby(['player_id', 'season_id'])     
  </code></pre>
  <p>I have the following function that I want to apply to each group:</p>
  <pre><code>def previous_mean(player_season):   
      avgs = {}
      i = 0
      for idx, game in player_season.iterrows():
          gamenum = i + 1
  
          if gamenum == 1:
              avgs[1] = 0
  
          elif gamenum == 2:
              avgs[2] = player_season.at[idx-1, 'dk_points']
          
          elif gamenum &gt; 2:
              logging.debug(&quot;gamenum is {0}&quot;.format(gamenum))
              pts = player_season.at[idx-1, 'points']
              avgs[gamenum] = (avgs.get(i)*(i-1) + pts)/i
  
          i+= 1
      
      return avgs.values()
  </code></pre>
  <p>Calling</p>
  <pre><code>grouped.apply(previous_mean)
  </code></pre>
  <p>results in the following:</p>
  <pre><code>player_id  season_id
  200        21200        [0, 10, 15.0]
             21999              [0, 10]
  300        21999                  [0]
             22111        [0, 10, 15.0]
  </code></pre>
  <p>How do I make the results of the apply operation the values of the &quot;mean_to_date&quot; column? That is, the mean_to_date for player 200, season 21999 would be 0 and 10, then for player 200, season 21200 it would be 0, 10, and 15, and so forth. Note that the mean_to_date value represents the mean prior to the game, so before the 1st game it is zero, and before the second game it is the total from the first game.</p>
  <p>Also, the &quot;previous_mean&quot; function is ugly and there is probably a more efficient way to accomplish the same end, but I couldn't figure it.</p>