Search
 
SCRIPT & CODE EXAMPLE
 

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)
Comment

PREVIOUS NEXT
Code Example
Python :: beautifulsoup find 
Python :: pandas sort dataframe by index 
Python :: media pipe install ERROR: Could not find a version that satisfies the requirement mediapipe (from versions: none) 
Python :: read excel date in python 
Python :: scrapy get inside attribute value 
Python :: python circular import 
Python :: how to calculate the variance of all columns in python 
Python :: how to copy file from local to sftp using python 
Python :: django oauth toolkit permanent access token 
Python :: python string cut first n characters 
Python :: change forms labels django 
Python :: how to make a python terminal 
Python :: beautifulsoup find element by partial text 
Python :: to str python 
Python :: how to download packages using pip 
Python :: how to make every letter capital in python 
Python :: pyinstaller onefile current working directory 
Python :: python get column from grouped dataframe 
Python :: install python 3.8 on wsl 
Python :: os.chdir go back 
Python :: turtle with python 
Python :: python crear dataframe 
Python :: list in list python 
Python :: HUNGRY CHEF codechef 
Python :: disable gpu in jupyter notebook in tensorflow 
Python :: plot data python 
Python :: sql like equivalent in python 
Python :: get channle from id discord.py 
Python :: how to change todays date formate in python 
Python :: list to csv python 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =