📅  最后修改于: 2022-03-11 14:46:51.377000             🧑  作者: Mango
df.groupby('id').apply(lambda x : x.sort_values(by = 'value', ascending = False).head(2).reset_index(drop = True))
#Alter native
df.groupby(['id']).apply(lambda x: x.nlargest(topk,['value'])).reset_index(drop=True)