按多Pandas分组排序

2024-03-28 19:10:16 发布

您现在位置:Python中文网/ 问答频道 /正文

给定以下数据帧:

import pandas as pd
df=pd.DataFrame({'County':['A','B','C','D','A','B','C','D','A','B','C','D','A','B','C','D','A','B'],
                'Hospital':['a','b','c','d','e','a','b','c','e','a','b','c','d','e','a','b','c','e'],
                'Enrollment':[44,55,42,57,95,54,27,55,81,54,65,23,89,76,34,12,1,67],
                'Year':['2012','2012','2012','2012','2012','2012','2012','2012','2012','2013',
                        '2013','2013','2013','2013','2013','2013','2013','2013']})
d2=pd.pivot_table(df,index=['County','Hospital'],columns=['Year'])#.sort_columns

d2
        Enrollment
        Year   2012     2013
County  Hospital        
A       a      44.0     NaN
        c      NaN      1.0
        d      NaN      89.0
        e      88.0     NaN
B       a      54.0     54.0
        b      55.0     NaN
        e      NaN      71.5
C       a      NaN      34.0
        b      27.0     65.0
        c      42.0     NaN
D       b      NaN      12.0
        c      55.0     23.0
        d      57.0     NaN

我需要对数据框进行排序,以便按照最近一年的入学人数(我想避免直接使用“2013”)对郡进行降序排序,如下所示:

^{pr2}$

然后,我希望每个县的医院都能按降序排序,但2013年的注册人数如下:

        Enrollment  
        Year    2012    2013
County  Hospital        
B       e       NaN 71.5
        a       54  54
        b       55  NaN
C       b       27  65
        a       NaN 34
        c       42  NaN
A       d       NaN 89
        c       NaN 1
        a       44  NaN
        e       88  NaN
D       c       55  23
        b       NaN 12
        d       57  NaN

到目前为止,我已经尝试使用groupby来获取总额并将其合并回来,但没有任何运气:

d2.groupby('County').sum()

提前谢谢!在


Tags: columns数据importpandasdf排序nanyear