Answers for "pandas groupby order"

1

pandas sort values group by

df.groupby(['job']).apply(lambda x: (x.groupby('source')
                                      .sum()
                                      .sort_values('count', ascending=False))
                                     .head(3))
Posted by: Guest on June-04-2020
0

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))
Posted by: Guest on August-18-2020
-1

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
Posted by: Guest on October-18-2020
0

Group based sort pandas

df_scored.sort_values('Score', ascending= False).sort_index(level='Index1', sort_remaining=False)
Posted by: Guest on April-29-2021

Python Answers by Framework

Browse Popular Code Answers by Language