我试图基于groupby的另一列获取具有最大值的行,我尝试遵循此处给出的解决方案Python:使用groupby获取具有最大值的行,但是当您应用该行时不起作用
annotations.groupby(['bookid','conceptid'], sort=False)['weight'].max()
我懂了
bookid conceptid
12345678 3942 0.137271
10673 0.172345
1002 0.125136
34567819 44407 1.370921
5111 0.104729
6160 0.114766
200 0.151629
3504 0.152793
但我只想获得权重最高的行,例如,
bookid conceptid
12345678 10673 0.172345
34567819 44407 1.370921
我将不胜感激
如果您需要最大重量的书号和概念号,请尝试以下操作
annotations.ix[annotations.groupby(['bookid'], sort=False)['weight'].idxmax()][['bookid', 'conceptid', 'weight']]
注意:由于ix
不推荐使用Pandas v0.20 。使用.loc
代替。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句