Pandas : Get top n records based on the sum of every top i record for each group
<p>I have a pandas dataframe like this :</p>

<pre><code>&gt;&gt;&gt; df

    id    value   
0    1      10
1    1      11
2    1      9
3    2      7
4    2      7
5    2      8
6    3      10
7    3      8
</code></pre>

<p>I want to get the top two id, based on the sum of their top two values.
So here, I should get this :</p>

<pre><code>    id    # value   
0    1    # 11 + 10 = 21
1    3    # 10 + 8 = 18
</code></pre>

<p>I tried using : </p>

<pre><code>df.groupby('id')['value'].nlargest(2).sum()
</code></pre>

<p>But that returns the sum of all the largest values.</p>

<p>I looked for an answer to my question, but I didn't quite get the proper one. </p>