2014-11-06 40 views
1

荫试图让根据GROUPBY的另一列与最大值行,我试图遵从下面给出Python : Getting the Row which has the max value in groups using groupby的解决方案,然而,当你运用它不起作用如何获得多列组的最大值 - by pandas?

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 

我想感谢所有帮助

+1

只是一个想法,就这样给你你想要的东西:'annotations.groupby( ['bookid'],sort = False)['weight']。max()' – EdChum 2014-11-07 08:59:48

回答

4

如果你需要的最大重量的BOOKID和conceptid,试试这个

annotations.ix[annotations.groupby(['bookid'], sort=False)['weight'].idxmax()][['bookid', 'conceptid', 'weight']] 
1

根据你想要的例子,我认为你的团队中有太多东西。我想你只需要:

annotations.groupby(['bookid'], sort=False)['weight'].max()