简体   繁体   English

在对熊猫数据框索引进行排序时,“ TypeError:'DataFrame'对象是可变的,因此无法进行散列”

[英]“TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed” while sorting pandas dataframe index

I have a following dataframe h : 我有以下数据框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'))

This throws an error: 这将引发错误:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

What am I missing? 我想念什么? Any ideas? 有任何想法吗?

Is that what you want? 那是你要的吗?

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

I think your index is filename. 我认为您的索引是文件名。 Maybe you could try something like: 也许您可以尝试以下方法:

h['index1'] = h.index
h.sort_values(by=['index1', 'age'])

But also it will not make so much sense since it will not change the order. 但这也没有太大意义,因为它不会改变顺序。 Alternatively you can try: 或者,您可以尝试:

h.sort_values(by='age')

Then: 然后:

h.reindex([range(some_number)])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:'DataFrame'对象是可变的,因此它们不能被散列 - TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed 在 dataframe 类型错误中出现错误:“系列”对象是可变的,因此无法对其进行哈希处理 - Getting error in dataframe typeError: 'Series' objects are mutable, thus they cannot be hashed 系列对象是可变的,因此它们不能在 Python pandas 数据帧上散列 - Series objects are mutable, thus they cannot be hashed on Python pandas dataframe TypeError: 'DataFrame' 对象是可变的,因此它们不能被散列 - 在创建数据帧字典时 - TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed- when creating dictionary of dataframes 当我在dataframe(pandas)中设置值时出现错误:“系列”对象是可变的,因此无法进行哈希处理 - when I set value in dataframe(pandas) there is error: 'Series' objects are mutable, thus they cannot be hashed DataFrame对象是可变的,因此在使用Series.unique()时无法对它们进行哈希处理 - DataFrame objects are mutable thus they cannot be hashed while using Series.unique() 类型错误:“系列”对象是可变的,因此它们不能被散列 - TypeError: 'Series' objects are mutable, thus they cannot be hashed 无法将自定义 function 二重唱应用到“DataFrame”对象是可变的,因此它们不能被散列 - Cannot apply custom function duet to 'DataFrame' objects are mutable, thus they cannot be hashed 在pandas系列上使用apply方法获取TypeError'Series'对象是可变的,因此不能将它们散列 - Using apply method on pandas series getting TypeError 'Series' objects are mutable, thus they cannot be hashed 如何修复 TypeError:'Series' 对象是可变的,因此它们不能被散列 - How to fix TypeError: 'Series' objects are mutable, thus they cannot be hashed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM