Python pandas equivalent to R groupby mutate
<p>So in R when I have a data frame consisting of say 4 columns, call it <code>df</code> and I want to compute the ratio by sum product of a group, I can it in such a way:</p>

<pre><code>// generate data
df = data.frame(a=c(1,1,0,1,0),b=c(1,0,0,1,0),c=c(10,5,1,5,10),d=c(3,1,2,1,2));
| a   b   c    d |
| 1   1   10   3 |
| 1   0   5    1 |
| 0   0   1    2 |
| 1   1   5    1 |
| 0   0   10   2 |
// compute sum product ratio
df = df%&gt;% group_by(a,b) %&gt;%
      mutate(
          ratio=c/sum(c*d)
      );
| a   b   c    d  ratio |
| 1   1   10   3  0.286 |
| 1   1   5    1  0.143 |
| 1   0   5    1  1     |
| 0   0   1    2  0.045 |
| 0   0   10   2  0.454 |
</code></pre>

<p>But in python I need to resort to loops.
I know there should be a more elegant way than raw loops in python, anyone got any ideas? </p>