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

Group based sort pandas

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

sort values within groups pandas dataframe

import pandas as pd

df = pd.DataFrame({'A':[1, 2, 3, 4, 5, 6],
                   'B':[0, 1, 2, 0, 1, 2]})

df.groupby('B').apply(lambda x: x.sort_values('A', ascending = True))
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 :: How to filter with Regex in Django ORM 
Python :: how to convert string to datetime 
Python :: python palindrome check 
Python :: list of dict to dict python 
Python :: how to check if python is installed on mac 
Python :: .first() in django 
Python :: python is instance 
Python :: var_dump in python 
Python :: interface, abstract python? 
Python :: is fastapi better than flask 
Python :: reverse python dictionary 
Python :: Customize color stacked bar chart matplotlib 
Python :: fizz buzz in python 
Python :: make value of two tables unique django 
Python :: if key in dictionary python 
Python :: string list to list 
Python :: tic tac toe minimax 
Python :: python jinja2 from string 
Python :: stack in python 
Python :: convert excel workbook to dataframe 
Python :: django background_task 
Python :: pandas make dataframe from few colums 
Python :: python split string with a seperator 
Python :: python discord know message from bot 
Python :: .counter python 
Python :: python search in json file 
Python :: pyqt5 drop down menu 
Python :: max value pandas 
Python :: csv in python 
Python :: use mark down with flask 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =