为每个组排序一次pandas multiindex

时间:2017-11-30 02:48:35

标签: python pandas

这是df:

<video autoplay="" width="640" height="360">
    <source src="http://127.0.0.1:5003" type="video/ogg">
    Your browser does not support the video tag.
</video>

预期产出是:

                    'A'         'B
Gain/Expsr% -10.0   2.104000    1.194000
            -2.0    1.389000    0.892000

Gain/MaxDD  -10.0   0.024000    0.064000
            -2.0    0.020000    0.058000

我尝试了一点点成功:

                    'A'         'B
Gain/Expsr%  -2.0   1.389000    0.892000
             -10.0  2.104000    1.194000

Gain/MaxDD  -2.0    0.020000    0.058000
            -10.0   0.024000    0.064000

以及以下内容:

 df.sortlevel(axis=1,level=[1,0],sort_remaining=True)
 df.sort_index(axis=1,level=1,sort_remaining=True)

1 个答案:

答案 0 :(得分:1)

让我们尝试使用ascending作为列表:

df.sort_index(level=[0,1], ascending=[True,True])

                       A      B
Gain/Expsr% -10.0  2.104  1.194
            -2.0   1.389  0.892
Gain/MaxDD  -10.0  0.024  0.064
            -2.0   0.020  0.058

df.sort_index(level=[0,1], ascending=[True, False])

                       A      B
Gain/Expsr% -2.0   1.389  0.892
            -10.0  2.104  1.194
Gain/MaxDD  -2.0   0.020  0.058
            -10.0  0.024  0.064