Lisa
Lisa

Reputation: 4416

Decided the order of the groupby output?

After the groupby, the output order of the group is pre-decided. In the following case, the order is A, AAA, B, BBB.

Is there a way to customize this order? I want to order to be AAA,A,BBB,B. I might want it in other orders as well.

import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
data=pd.DataFrame({'Rating':['A','AAA','B','BBB','A','AAA','B','BBB'],
                  'Score':[2,4,5,6,2,4,5,6,]})
t=data.groupby('Rating', sort=False)['Score'].mean()
t
Rating
A      2
AAA    4
B      5
BBB    6
Name: Score, dtype: int64

Upvotes: 0

Views: 87

Answers (2)

unutbu
unutbu

Reputation: 879251

You can't change the order returned by groupby/mean (save what's possible with the sort parameter). However, it easy to change the order after the fact using reindex:

In [24]: data.groupby('Rating', sort=False)['Score'].mean().reindex(['AAA', 'A', 'BBB', 'B'])
Out[24]: 
Rating
AAA    4
A      2
BBB    6
B      5
Name: Score, dtype: int64

Alternatively, you can control the order returned by groupby/mean by changing Ratings to a Categorical:

import pandas as pd

data = pd.DataFrame({'Rating':['A','AAA','B','BBB','A','AAA','B','BBB'],
                  'Score':[2,4,5,6,2,4,5,6,]})
data['Rating'] = pd.Categorical(data['Rating'], categories=['AAA','A','BBB','B'], 
                                ordered=True)
result = data.groupby('Rating', sort=False)['Score'].mean()
print(result)

yields

Rating
AAA    4
A      2
BBB    6
B      5
Name: Score, dtype: int64

Upvotes: 2

Andy Hayden
Andy Hayden

Reputation: 375415

sort=False just means that it's not guaranteed to be sorted (it may be ordered). My recollection is that this is in the "seen order", but again that's no guaranteed.

To sort the output of a groupby, just do the sort after (by the index):

In [11]: t.sort_index()
Out[11]:
Rating
A      2
AAA    4
B      5
BBB    6
Name: Score, dtype: int64

Upvotes: 0

Related Questions