SowD
SowD

Reputation: 101

Sort a column within groups in Pandas

I am new to pandas. I'm trying to sort a column within each group. So far, I was able to group first and second column values together and calculate the mean value in third column. But I am still struggling to sort 3rd column.

This is my input dataframe This is my dataframe after applying groupby and mean function

I used the following line of code to group input dataframe, df_o=df.groupby(by=['Organization Group','Department']).agg({'Total Compensation':np.mean})

Please let me know how to sort the last column for each group in 1st column using pandas.

Upvotes: 0

Views: 1771

Answers (1)

jezrael
jezrael

Reputation: 862521

It seems you need sort_values:

#for return df add parameter as_index=False
df_o=df.groupby(['Organization Group','Department'], 
                 as_index=False)['Total Compensation'].mean()
df_o = df_o.sort_values(['Total Compensation','Organization Group'])

Sample:

df = pd.DataFrame({'Organization Group':['a','b','a','a'],
                   'Department':['d','f','a','a'],
                   'Total Compensation':[1,8,9,1]})

print (df)
  Department Organization Group  Total Compensation
0          d                  a                   1
1          f                  b                   8
2          a                  a                   9
3          a                  a                   1

df_o=df.groupby(['Organization Group','Department'], 
                as_index=False)['Total Compensation'].mean()
print (df_o)
  Organization Group Department  Total Compensation
0                  a          a                   5
1                  a          d                   1
2                  b          f                   8

df_o = df_o.sort_values(['Total Compensation','Organization Group'])
print (df_o)
  Organization Group Department  Total Compensation
1                  a          d                   1
0                  a          a                   5
2                  b          f                   8

Upvotes: 1

Related Questions