如何按特定列对多级熊猫数据帧进行排序?

时间:2020-06-05 09:19:34

标签: python pandas

我想按列对多索引熊猫数据框进行排序,但不希望对整个数据框立即进行排序。而是想按索引之一进行排序。这是我的意思的示例:下面是多索引数据框的示例。

first  second
bar    one       0.361041
       two       0.476720
baz    one       0.565781
       two       0.848519
foo    one       0.405524
       two       0.882497
qux    one       0.488229
       two       0.303862

我想要的是:

first  second
bar    two       0.476720
       one       0.361041
baz    two       0.848519
       one       0.565781
foo    two       0.882497
       one       0.405524
qux    two       0.488229
       one       0.303862

我想根据第三列(即数字)对数据框进行排序,并保持0级索引不变。我们该怎么做?

2 个答案:

答案 0 :(得分:1)

Series.reindex与排序的第二级值一起使用:

a = s.reindex(sorted(s.index.levels[1], reverse=True), level=1)
print (a)
first  second
bar    two       0.476720
       one       0.361041
baz    two       0.848519
       one       0.565781
foo    two       0.882497
       one       0.405524
qux    two       0.303862
       one       0.488229
Name: a, dtype: float64

答案 1 :(得分:0)

您可以使用sort index对索引进行排序,并将redirect_uri的布尔值传递给export const setDecimalFormat = amount => { return ['de' , 'fr'].includes(getLocale()) ? amount.replace(/\./g, ',') : amount; }; 参数

[True,False]