title: Difference to group mean in a pandas data frame?
body: <p>Lets's assume I count how many oranges (<code>Orange</code>) and apples (<code>Apple</code>) people (<code>id</code>) eat in a certain time period. I also know if they are young or old (<code>group</code>). The pandas dataframe would maybe look like this:</p>
  <pre><code>df = pd.DataFrame({'id': ['1','2','3','7'],
                  'group': ['Young', 'Young', 'Old', 'Old'],
                  'Apple': [7,2,5,4],
                 'Orange': [3,6,4,4] })
  
    id  group  Apple  Orange
  0  1  Young      7       3
  1  2  Young      2       6
  2  3    Old      5       4
  3  7    Old      4       4
  </code></pre>
  <p>We can easily compute means using <code>groupby()</code>. E. g:</p>
  <pre><code>df.Apple.groupby(df.group).mean()
  </code></pre>
  <p>outputs</p>
  <pre><code>Old      4.5
  Young    4.5
  </code></pre>
  <p>But let's say, I want to find how much the amount of apples and oranges consumed differs to the group mean per individual?</p>
  <p>That is, the output should be</p>
  <pre><code>df = pd.DataFrame({'id': ['1','2','3','7'],
                  'group': ['Young', 'Young', 'Old', 'Old'],
                  'Apple': [7,2,5,4],
                 'Orange': [3,6,4,4],
       'Apple Difference': [2.5, -2.5, 0.5, -0.5] })
  
    id  group  Apple  Orange  Apple Difference
  0  1  Young      7       3               2.5
  1  2  Young      2       6              -2.5
  2  3    Old      5       4               0.5
  3  7    Old      4       4              -0.5
  </code></pre>
  <p>Is there a way to do this with pandas/numpy? Sorry for the rockie-question Best /R</p>