我是熊猫的新手。我正在尝试对每个组中的列进行排序。到目前为止,我能够将第一列和第二列值组合在一起并计算第三列中的平均值。但我仍然在努力排序第三列。
This is my input dataframe This is my dataframe after applying groupby and mean function
我使用以下代码行对输入数据帧进行分组, df_o = df.groupby(by = ['Organization Group','Department'])。agg({'Total Compensation':np.mean})
请告诉我如何使用pandas为第1列中的每个组排序最后一列。
答案 0 :(得分:1)
您似乎需要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'])
样品:
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