title: apply() Method in Pandas
body: <p>anyone here know what is actually done by these codes below? I got confused that they produces different result on 'data1'.</p>
  
  <p>Here is the first code:</p>
  
  <pre><code>def norm_by_data2(x):
      # x is a DataFrame of group values
      x['data1'] /= x['data2'].sum()
      return x
  
  print(df, "\n")
  print(df.groupby('key').apply(norm_by_data2), "\n")
  </code></pre>
  
  <p>Here is the second code:</p>
  
  <pre><code>pd.Series(df['data1'] / df['data2'].sum(), name='data1')
  </code></pre>
  
  <p>Output of first code:</p>
  
  <pre><code># How the DataFrame looks like
    key  data1  data2
  0   A      0      5
  1   B      1      0
  2   C      2      3
  3   A      3      3
  4   B      4      7
  5   C      5      9 
  
    key     data1  data2
  0   A  0.000000      5
  1   B  0.142857      0
  2   C  0.166667      3
  3   A  0.375000      3
  4   B  0.571429      7
  5   C  0.416667      9 
  </code></pre>
  
  <p>Output of second code:</p>
  
  <pre><code>0    0.000000
  1    0.037037
  2    0.074074
  3    0.111111
  4    0.148148
  5    0.185185
  Name: data1, dtype: float64
  </code></pre>