0

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.

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting following error"KeyError: 'Organization Group'"
I want the output to be in following format Organization_group Department Total Compensation a a 5 no val d 1 b f 8

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.