Python 按不同级别对熊猫中的多个索引进行不同排序
我有一个索引为3级的数据帧。我需要按每个级别对索引进行排序,但方式不同。什么可以做到这一点 将数据帧(Python 按不同级别对熊猫中的多个索引进行不同排序,python,pandas,sorting,dataframe,multi-index,Python,Pandas,Sorting,Dataframe,Multi Index,我有一个索引为3级的数据帧。我需要按每个级别对索引进行排序,但方式不同。什么可以做到这一点 将数据帧(df)设置为: 我想要一个新的df,其中color排序为升序,形状排序为降序,计数排序为升序: other columns color shape count blue triangle 2 x circle 4 x re
df
)设置为:
我想要一个新的df
,其中color
排序为升序
,形状
排序为降序
,计数
排序为升序
:
other columns
color shape count
blue triangle 2 x
circle 4 x
red triangle 2 x
3 x
circle 1 x
将
升序
参数与布尔值列表一起使用:
df.sort_index(ascending=[True, False, True])
输出:
other columns
color shape count
blue triangle 2 x
circle 4 x
red triangle 2 x
3 x
circle 1 x
other columns
color shape count
blue triangle 2 x
circle 4 x
red triangle 2 x
3 x
circle 1 x