且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按列对数据框多索引级别进行排序

更新时间:2023-01-29 15:52:00

以下是您需要的一些潜在解决方案:

Here are some potential solutions for your needs:

方法1:

 (df.sort_values('value_1', ascending=False)
    .sort_index(level=[0], ascending=[True]))

方法2:

 (df.set_index('value_1', append=True)
    .sort_index(level=[0,2], ascending=[True,False])
    .reset_index('value_1'))

在pandas 0.22.0,Python 3.6.4上进行了测试

Tested on pandas 0.22.0, Python 3.6.4