缥缈止盈
简单的解决办法是:函数获取具有最大值的行的索引。这将筛选出组中具有最大值的所有行。In [365]: import pandas as pdIn [366]: df = pd.DataFrame({'sp' : ['MM1', 'MM1', 'MM1', 'MM2', 'MM2', 'MM2', 'MM4', 'MM4','MM4'],'mt' : ['S1', 'S1', 'S3', 'S3', 'S4', 'S4', 'S2', 'S2', 'S2'],'val' : ['a', 'n', 'cb', 'mk', 'bg', 'dgb', 'rd', 'cb', 'uyi'],'count' : [3,2,5,8,10,1,2,2,7]})In [367]: df Out[367]: count mt sp val0 3 S1 MM1 a1 2 S1 MM1 n2 5 S3 MM1 cb3 8 S3 MM2 mk4 10 S4 MM2 bg5 1 S4 MM2 dgb6 2 S2 MM4 rd7 2 S2 MM4 cb8 7 S2 MM4 uyi### Apply idxmax() and use .loc() on dataframe to filter the rows with max values:In [368]: df.loc[df.groupby(["sp", "mt"])["count"].idxmax()] Out[368]: count mt sp val0 3 S1 MM1 a2 5 S3 MM1 cb3 8 S3 MM2 mk4 10 S4 MM2 bg8 7 S2 MM4 uyi### Just to show what values are returned by .idxmax() above:In [369]: df.groupby(["sp", "mt"])["count"].idxmax().values Out[369]: array([0, 2, 3, 4, 8])