【发布时间】:2020-12-13 15:38:32
【问题描述】:
所以我有一个具有多级多索引的数据框,其中较小的索引是父级,添加一级索引会创建看起来像这样的子级
(a,foo,1)
(a,foo,2)
(a,foo)
a
(b, foo,1)
(b, bar,1)
(b, foo)
(b, bar)
b
我想对索引进行排序,但是在使用 sort_index 函数时,我收到以下错误,
TypeError: '<' not supported between instances of 'str' and 'tuple'
由于任何大于 1 级的索引都存储为元组,而单个索引存储为字符串,我无法对索引进行排序。
让我的索引成为单一数据类型以便我对其进行排序的最佳方法是什么?
a
(a,foo)
(a,foo,1)
(a,foo,2)
b
(b, bar)
(b, bar,1)
(b, foo)
(b, foo,1)
【问题讨论】:
标签: python pandas sorting multi-index