“系列对象是可变的,不能被散列"错误 [英] "Series objects are mutable and cannot be hashed" error

查看:61
本文介绍了“系列对象是可变的,不能被散列"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使以下脚本工作.输入文件由3列组成:基因关联类型、基因名称和疾病名称.

I am trying to get the following script to work. The input file consists of 3 columns: gene association type, gene name, and disease name.

cols = ['Gene type', 'Gene name', 'Disorder name']
no_headers = pd.read_csv('orphanet_infoneeded.csv', sep=',',header=None,names=cols)

gene_type = no_headers.iloc[1:,[0]]
gene_name = no_headers.iloc[1:,[1]]
disease_name = no_headers.iloc[1:,[2]]

query = 'Disease-causing germline mutation(s) in' ###add query as required

orph_dict = {}

for x in gene_name:
    if gene_name[x] in orph_dict:
        if gene_type[x] == query:
            orph_dict[gene_name[x]]=+ 1
        else:
            pass
    else:
        orph_dict[gene_name[x]] = 0

我不断收到一条错误消息:

I keep getting an error that says:

系列对象是可变的,不能被散列

Series objects are mutable and cannot be hashed

非常感谢任何帮助!

推荐答案

Shortly: gene_name[x] 是一个可变对象,所以它不能被散列.要将对象用作字典中的键,python 需要使用其哈希值,这就是为什么会出错.

Shortly: gene_name[x] is a mutable object so it cannot be hashed. To use an object as a key in a dictionary, python needs to use its hash value, and that's why you get an error.

进一步说明:

可变对象是可以改变值的对象.例如, list 是一个可变对象,因为您可以附加到它.int 是一个不可变的对象,因为你不能改变它.当你这样做时:

Mutable objects are objects which value can be changed. For example, list is a mutable object, since you can append to it. int is an immutable object, because you can't change it. When you do:

a = 5;
a = 3;

您不会更改 a 的值,而是创建一个新对象并使 a 指向其值.

You don't change the value of a, you create a new object and make a point to its value.

无法对可变对象进行哈希处理.请参阅此答案.

Mutable objects cannot be hashed. See this answer.

要解决您的问题,您应该使用不可变对象作为字典中的键.例如:tuplestringint.

To solve your problem, you should use immutable objects as keys in your dictionary. For example: tuple, string, int.

这篇关于“系列对象是可变的,不能被散列"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆