Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas sort values group by

df.groupby(['job']).apply(lambda x: (x.groupby('source')
                                      .sum()
                                      .sort_values('count', ascending=False))
                                     .head(3))
Comment

groupby and sort python

In[34]: df.sort_values(['job','count'],ascending=False).groupby('job').head(3)

Out[35]: 
   count     job source
4      7   sales      E
2      6   sales      C
1      4   sales      B
5      5  market      A
8      4  market      D
6      3  market      B
Comment

Group based sort pandas

df_scored.sort_values('Score', ascending= False).sort_index(level='Index1', sort_remaining=False)
Comment

pandas sort values in groupby

In[34]: df.sort_values(['job','count'],ascending=False).groupby('job').head(3)

Out[35]: 
   count     job source
4      7   sales      E
2      6   sales      C
1      4   sales      B
5      5  market      A
8      4  market      D
6      3  market      B
Comment

PREVIOUS NEXT
Code Example
Python :: python 
Python :: read csv pandas 
Python :: how to find in which directory my python code is running 
Python :: python password with special characters 
Python :: pygame tutorial 
Python :: python binary string to int 
Python :: install coverage python 
Python :: Python JSON API example 
Python :: run python.py file 
Python :: python array append 
Python :: how to remove quasi constant column in pandas dataframe 
Python :: dictionary to list python 
Python :: start django project in windows 
Python :: how to access dataframe row by datetime index 
Python :: python rgb to hex 
Python :: python script as service linux 
Python :: get ip address py 
Python :: dropna threshold 
Python :: Converting Dataframe from the multi-dimensional list 
Python :: if main 
Python :: python file hashlib 
Python :: how to make a nan value in a list 
Python :: roman to integer 
Python :: how to extract words from string in python 
Python :: how to show a frequency distribution based on date in python 
Python :: connectionrefusederror at /accounts/signup/ django allauth 
Python :: adding proxy in selenium python 
Python :: print first word of a string python and return it 
Python :: how to convert each string to a category or int in python dataframe 
Python :: if substring not in string python 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =