【问题标题】:Ascending order sorting in Dataframe-Pandas after GroupByGroupBy之后Dataframe-Pandas中的升序排序
【发布时间】:2017-08-30 11:18:51
【问题描述】:
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 列进行排序。并在此基础上,部门应在每个组织组内进行更改。组织组列不应更改。

【问题讨论】:

标签: python-2.7 pandas numpy dataframe


【解决方案1】:

您可以将sort_valuessort_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_indexsort_valuesset_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

【讨论】:

  • 我有更多关于提取功能的问题
  • df['Awards'].str.extract('Won (\d+) Oscar([s]+)', expand=True).fillna(0).有时数据只有奥斯卡,有些数据有奥斯卡。如何在上述表达式中匹配正则表达式。
  • 嗯,我认为最好的方法是创建新问题 - 只需获取数据样本、所需输出和您的代码 (df['Awards'].str.extract('Won (\d+) Oscar([s]+)', expand=True).fillna(0))。我不是正则表达式专家,但我认为有人可以帮助你。因为没有数据是不可能的答案。
猜你喜欢
  • 1970-01-01
  • 2017-01-09
  • 2017-07-04
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
相关资源
最近更新 更多