I am trying to get the row index with the second highest value after groupby is done, but I am not getting the correct result
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]})
Doing this
df.iloc[df.groupby(['Mt'])['Value'].apply(lambda x: (x!=max(x)).idxmax())]
returns
Mt Sp Value count
0 s1 a 1 3
2 s2 c 3 5
5 s3 f 6 6
For group s2, it is necessary to return index 3 of the original data block.
source
share