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

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 :: scikit decision tree regressor 
Python :: python function to multiply two numbers 
Python :: python / vs // 
Python :: python difference 
Python :: python label 
Python :: django rest framework viewset 
Python :: python string caps lock 
Python :: true in python 
Python :: bounding box in matplotlib 
Python :: install python 3.7 
Python :: how to mention someone discord.py 
Python :: Tree: Inorder Traversal 
Python :: how to access variable of one function in another function in python 
Python :: reduce () in python 
Python :: python check if character in string 
Python :: how to store a return value in a variable in python 
Python :: How to select element using xpath in python 
Python :: scaling 
Python :: how to get data from django session 
Python :: data type 
Python :: Issue AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’ 
Python :: tkinter bg button 
Python :: iteration over dictionary 
Python :: python true and false 
Python :: python create empty list 
Python :: infinite for loop in python 
Python :: round() function in python 
Python :: pythagore 
Python :: comment multiple lines python 
Python :: editing specific line in text file in python 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =