Сортировка Мульти Индекса в Пандах по-разному для каждого уровня

1

У меня есть dataframe с индексом 3 уровня. Мне нужно сортировать индекс по каждому уровню, но по-разному. Что можно достичь?

Имейте dataframe (df) как:

                     other columns
color shape    count              
red   circle   1                 x
      triangle 3                 x
               2                 x
blue  circle   4                 x
      triangle 2                 x

и я хочу новый df где color сортируется по ascending, shape descending, а count ascending:

                     other columns
color shape       count              
blue  triangle    2                 x
      circle      4                 x
red   triangle    2                 x
                  3                 x
      circle      1                 x
Теги:
pandas
dataframe
sorting
multi-index

1 ответ

4
Лучший ответ

Используйте ascending параметр со списком булевых:

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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню