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 :: how to move a column to last in pandas 
Python :: from .cv2 import * ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8 
Python :: add numpy array to pandas dataframe 
Python :: add jupyter environment 
Python :: how to run function on different thread python 
Python :: python -m pip install 
Python :: random forrest plotting feature importance function 
Python :: pandas read csv as strings 
Python :: email authentication python 
Python :: discord python bot require one of two roles for command 
Python :: prime number program in python print 1 to 100 
Python :: python for loop backwards 
Python :: torch save 
Python :: exoort csv google colab 
Python :: how to set index pandas 
Python :: How can one find the three largest values of an input array efficiently, namely without having to sort the input array? 
Python :: read text from a pdffile python 
Python :: check pip installed packages inside virtualenv 
Python :: how to delete a turtle in python 
Python :: pandas groupby get all but first row 
Python :: cvtcoloer opencv 
Python :: How to create a hyperlink with a Label in Tkinter 
Python :: Consider using python 3 style super without arguments 
Python :: how to get height in pyqt5 
Python :: python current utc offset 
Python :: how to make a button circular in python 
Python :: write json to file python 
Python :: sns legend outside 
Python :: CUDA error: device-side assert triggered 
Python :: numpy set_printoptions 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =