擅长:python、mysql、java
<p>使用双<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.nlargest.html" rel="nofollow noreferrer">^{<cd1>}</a>-首先获取<code>2</code>顶部值,然后<code>sum</code>它们,然后再使用另一个前2个索引值:</p>
<pre><code>L = df.groupby('PrimaryName')['Value']
.apply(lambda x: x.nlargest(2).sum())
.nlargest(2)
.index
.tolist()
print (L)
['PN1', 'PN0']
</code></pre>
<p>详细信息:</p>
^{pr2}$
<p>或者:</p>
<pre><code>L = df.sort_values('Value', ascending=False)
.groupby('PrimaryName')['Value']
.apply(lambda x: x.head(2).sum())
.nlargest(2)
.index
.tolist()
</code></pre>