title: python pandas groupby() result
body: <p>I have the following python pandas data frame:</p>
  
  <pre><code>df = pd.DataFrame( {
     'A': [1,1,1,1,2,2,2,3,3,4,4,4],
     'B': [5,5,6,7,5,6,6,7,7,6,7,7],
     'C': [1,1,1,1,1,1,1,1,1,1,1,1]
      } );
  
  df
      A  B  C
  0   1  5  1
  1   1  5  1
  2   1  6  1
  3   1  7  1
  4   2  5  1
  5   2  6  1
  6   2  6  1
  7   3  7  1
  8   3  7  1
  9   4  6  1
  10  4  7  1
  11  4  7  1
  </code></pre>
  
  <p>I would like to have another column storing a value of a sum over C values for fixed (both) A and B. That is, something like:</p>
  
  <pre><code>    A  B  C  D
  0   1  5  1  2
  1   1  5  1  2
  2   1  6  1  1
  3   1  7  1  1
  4   2  5  1  1
  5   2  6  1  2
  6   2  6  1  2
  7   3  7  1  2
  8   3  7  1  2
  9   4  6  1  1
  10  4  7  1  2
  11  4  7  1  2
  </code></pre>
  
  <p>I have tried with pandas <code>groupby</code> and it kind of works:</p>
  
  <pre><code>res = {}
  for a, group_by_A in df.groupby('A'):
      group_by_B = group_by_A.groupby('B', as_index = False)
      res[a] = group_by_B['C'].sum()
  </code></pre>
  
  <p>but I don't know how to 'get' the results from <code>res</code> into <code>df</code> in the orderly fashion. Would be very happy with any advice on this. Thank you. </p>