"TypeError:"DataFrame"对象是可变的,因此不能被散列"在对 pandas 数据框索引进行排序时 [英] "TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed" while sorting pandas dataframe index
本文介绍了"TypeError:"DataFrame"对象是可变的,因此不能被散列"在对 pandas 数据框索引进行排序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框h
:
In [24]: h.head()
Out[24]:
alpha1 alpha2 gamma1 gamma2 chi2min gender age
filename
F35_HC_532d.dat 0.0000 0.000 NaN 0.00 1.000000e+25 F 35
M48_HC_551d.dat 0.7353 3.943 0.425922 0.15 2.072617e+01 M 48
M24_HC_458d.dat 0.7777 4.754 0.463753 0.15 1.390893e+01 M 24
M48_HC_552d.dat 0.7633 3.672 0.394370 0.15 1.965052e+01 M 48
M40_HC_506d.dat 0.7793 3.271 0.513597 0.20 1.089716e+01 M 40
我正在尝试根据年龄值对数据框索引进行排序:
I am trying to sort the dataframe index according to age values:
In [25]: h.sort_index(h.sort_values('age'))
这会引发错误:
TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed
我想念什么?有什么想法吗?
What am I missing? Any ideas?
推荐答案
这就是您想要的吗?
In [14]: h
Out[14]:
alpha1 alpha2 gamma1 gamma2 chi2min gender age
filename
F35_HC_532d.dat 0.0000 0.000 NaN 0.00 1.000000e+25 F 35
M48_HC_551d.dat 0.7353 3.943 0.425922 0.15 2.072617e+01 M 48
M24_HC_458d.dat 0.7777 4.754 0.463753 0.15 1.390893e+01 M 24
M48_HC_552d.dat 0.7633 3.672 0.394370 0.15 1.965052e+01 M 48
M40_HC_506d.dat 0.7793 3.271 0.513597 0.20 1.089716e+01 M 40
In [15]: h.sort_values('age')
Out[15]:
alpha1 alpha2 gamma1 gamma2 chi2min gender age
filename
M24_HC_458d.dat 0.7777 4.754 0.463753 0.15 1.390893e+01 M 24
F35_HC_532d.dat 0.0000 0.000 NaN 0.00 1.000000e+25 F 35
M40_HC_506d.dat 0.7793 3.271 0.513597 0.20 1.089716e+01 M 40
M48_HC_551d.dat 0.7353 3.943 0.425922 0.15 2.072617e+01 M 48
M48_HC_552d.dat 0.7633 3.672 0.394370 0.15 1.965052e+01 M 48
这篇关于"TypeError:"DataFrame"对象是可变的,因此不能被散列"在对 pandas 数据框索引进行排序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文