我正在尝试根据 groupby 的另一列获取具有最大值的行,我正在尝试遵循此处给出的解决方案 Python : Getting the Row which has the max value in groups using 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
如果有任何帮助,我将不胜感激
最佳答案
如果你需要最大权重的 bookid 和 conceptid,试试这个
annotations.ix[annotations.groupby(['bookid'], sort=False)['weight'].idxmax()][['bookid', 'conceptid', 'weight']]
注意自从 Pandas v0.20 ix
已被弃用。请改用 .loc
。
关于python - 如何获得多列分组 Pandas 的最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26789935/