Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply sort to a pandas groupby operation

Tags:

python

pandas

How do I apply sort to a pandas groupby operation? The command below returns an error saying that 'bool' object is not callable

import pandas as pd

df.groupby('cokey').sort('A')

cokey       A   B
11168155    18  56
11168155    0   18
11168155    56  96
11168156    96  152
11168156    0   96
like image 490
user308827 Avatar asked Apr 06 '15 20:04

user308827


People also ask

How do I sort a panda Groupby object?

To group Pandas dataframe, we use groupby(). To sort grouped dataframe in ascending or descending order, use sort_values(). The size() method is used to get the dataframe size.

How do you sort objects in Groupby?

Sort within Groups of groupby() Result in DataFrameBy using DataFrame. sort_values() , you can sort DataFrame in ascending or descending order, before you use this first group the DataFrame rows by using DataFrame. groupby() method. Note that groupby preserves the order of rows within each group.

How do you get Groupby descending in pandas?

To group Pandas dataframe, we use groupby(). To sort grouped dataframe in descending order, use sort_values(). The size() method is used to get the dataframe size.

Does Groupby maintain order pandas?

Groupby preserves the order of rows within each group. When calling apply, add group keys to index to identify pieces. Reduce the dimensionality of the return type if possible, otherwise return a consistent type.


1 Answers

Normally the sort is performed on the groupby keys and as you've found out you can't call sort on a groupby object, what you could do is call apply and pass the DataFrame.sort function and pass the column as the kwarg param:

In [58]:

df.groupby('cokey').apply(pd.DataFrame.sort, 'A')
Out[58]:
               cokey   A    B
cokey                        
11168155 1  11168155   0   18
         0  11168155  18   56
         2  11168155  56   96
         3  11168155  96  152

Alternatively you could just sort the df prior to grouping:

df.sort('A').groupby('cokey')

Update

For version 0.17.0 and above DataFrame.sort is now deprecated see the docs, one should now use DataFrame.sort_values:

df.groupby('cokey').apply(pd.DataFrame.sort_values, 'A')

Adding @xgdgsc 's answer in comments to here; in case you need to set ascending flag.

df.groupby('cokey').apply(pd.DataFrame.sort_values, 'A', ascending=False)
like image 65
EdChum Avatar answered Oct 07 '22 12:10

EdChum