Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #select #top #group #pandas
ADD COMMENT
Topic
Name
9+3 =