我试图在 groupby 后获取第二高值的行索引,但我没有得到正确的结果。
df = pd.DataFrame({'Sp':['a','b','c','d','e','f'], 'Mt':['s1', 's1', 's2','s2','s2','s3'], 'Value':[1,2,3,4,5,6], 'count':[3,2,5,10,10,6]})
这样做
df.iloc[df.groupby(['Mt'])['Value'].apply(lambda x: (x!=max(x)).idxmax())]
返回
Mt Sp Value count
0 s1 a 1 3
2 s2 c 3 5
5 s3 f 6 6
对于 group s2,应返回原数据帧的第3个索引。