熊猫对索引进行排序抛出"unhashable type: 'list'"错误



我创建我的数据集如下(有意失效(:

pa = pd.MultiIndex.from_tuples([('1', '2'),('2', '1'),('1', '4'),('1', '5'),('1', '3'),('2', '2'),('2', '3'),('3', '1'),('3', '2'),('1', '1')]
                           , names=['batch', 'run'])
yld=pd.DataFrame(np.random.randn(10,1),index=pa,columns=['yield'])

当我尝试在batch&amp上进行排序时run索引列:

yld.sort_index(['batch','run'])

我得到TypeError: unhashable type: 'list'

我不知道出了什么问题。

因为sort_index的位置从axis开始,然后level,然后您必须使用level=,请参见下面的sort_index签名:

签名:yld.sort_index(axis = 0,level = none,ascending = true,intplace = false = false ='quickSort',na_position ='last',sort_remaining = true,by = none = none(

因此,正确的语法是@Wen建议的。

yld.sort_index(level=['batch','run'])

yld.sort_index()

因为级别会自动在外部的索引级别上排序。

最新更新