from pandas import Series, DataFrame
import pandas as pd
df1=pd.read_csv('/Users/nirmal/Desktop/Python/Assignments/Data/employee_compensation.csv', sep=',', skiprows=(1,1))
dfq2=DataFrame(df1.groupby(["Organization Group", "Department"])['Total Compensation'].mean())
dfq2
我需要按顺序对Total Compensation列进行排序。并在此基础上,部门应在每个组织小组内进行变更。组织组列不应更改。
答案 0 :(得分:0)
您可以sort_values
使用sort_index
:
print (df.sort_values('Total Compensation', ascending=False)
.sort_index(level=0, sort_remaining=False))
Total Compensation
Organization Group Department
Community Health Academy of Sciences 107319.727692
Public Health 96190.190140
Arts Commission 94339.597388
Asian Art Museum 71401.520060
Culture & Recreation Law Library 188424.362222
City Attorney 166082.677561
Controller 104515.234944
Assessor/Recorder 89994.260614
City Planning 89022.876966
Board of Supervisors 78801.347641
War Memorial 76250.068022
Public Library 70446.352147
Civil Service Commission 67966.756559
Fine Arts Museum 44205.439895
Recreation and Park Commission 38912.859465
Elections 20493.166618
General Administration & Finance Ethics Commission 98631.380366
reset_index
,sort_values
和set_index
的另一种解决方案:
print (df.reset_index()
.sort_values(['Organization Group','Total Compensation'], ascending=[True, False])
.set_index(['Organization Group','Department']))
Total Compensation
Organization Group Department
Community Health Academy of Sciences 107319.727692
Public Health 96190.190140
Arts Commission 94339.597388
Asian Art Museum 71401.520060
Culture & Recreation Law Library 188424.362222
City Attorney 166082.677561
Controller 104515.234944
Assessor/Recorder 89994.260614
City Planning 89022.876966
Board of Supervisors 78801.347641
War Memorial 76250.068022
Public Library 70446.352147
Civil Service Commission 67966.756559
Fine Arts Museum 44205.439895
Recreation and Park Commission 38912.859465
Elections 20493.166618
General Administration & Finance Ethics Commission 98631.380366