Answers for "how to select top 5 in every group pandas"

0

how to select top 5 in every group pandas

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)
Posted by: Guest on April-29-2021

Python Answers by Framework

Browse Popular Code Answers by Language