如何通过MultiIndex和值对Pandas DataFrame进行排序? [英] How to sort Pandas DataFrame both by MultiIndex and by value?

查看:140
本文介绍了如何通过MultiIndex和值对Pandas DataFrame进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样本数据:

mdf = pd.DataFrame([[1,2,50],[1,2,20],
                [1,5,10],[2,8,80],
                [2,5,65],[2,8,10]
               ], columns=['src','dst','n']); mdf

    src dst n
0   1   2   50
1   1   2   20
2   1   5   10
3   2   8   80
4   2   5   65
5   2   8   10

groupby()给出了两个级别的多索引:

groupby() gives a two-level multi-index:

test = mdf.groupby(['src','dst'])['n'].agg(['sum','count']); test

        sum count
src dst 
1   2   70  2
    5   10  1
2   5   65  1
    8   90  2

问题:如何按src升序然后sum降序对该DataFrame排序?

Question: how to sort this DataFrame by src ascending and then by sum descending?

我是熊猫的初学者,了解了sort_index()和sort_values(),但是在此任务中,我似乎同时需要两者.

I'm a beginner with pandas, learned about sort_index() and sort_values(), but in this task it seems that I need both simultaneously.

预期结果,在每个"src"下的排序由"sum"确定:

Expected result, under each "src" sorting is determined by the "sum":

        sum count
src dst 
1   2   70  2
    5   10  1
2   8   90  2
    5   65  1

推荐答案

IIUC:

In [29]: test.sort_values('sum', ascending=False).sort_index(level=0)
Out[29]:
         sum  count
src dst
1   2     80      2
    5     10      1
2   8     80      1

更新:非常类似于 @anonyXmous的解决方案:

In [47]: (test.reset_index()
              .sort_values(['src','sum'], ascending=[1,0])
              .set_index(['src','dst']))
Out[47]:
         sum  count
src dst
1   2     70      2
    5     10      1
2   8     90      2
    5     65      1

这篇关于如何通过MultiIndex和值对Pandas DataFrame进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆