Python “系列对象是可变的,不能被散列”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29700552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:53:55  来源:igfitidea点击:

"Series objects are mutable and cannot be hashed" error

python

提问by Sal

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.

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

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

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

Any help would be dearly appreciated!

任何帮助将不胜感激!

回答by jkitchen

gene_name = no_headers.iloc[1:,[1]]

This creates a DataFrame because you passed a list of columns (single, but still a list). When you later do this:

这将创建一个 DataFrame,因为您传递了一个列列表(单个,但仍然是一个列表)。当您稍后执行此操作时:

gene_name[x]

you now have a Series object with a single value. You can't hash the Series.

您现在有一个具有单个值的 Series 对象。你不能散列系列。

The solution is to create Series from the start.

解决方案是从一开始就创建系列。

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

Also, where you have orph_dict[gene_name[x]] =+ 1, I'm guessing that's a typo and you really mean orph_dict[gene_name[x]] += 1to increment the counter.

另外,在你有的地方orph_dict[gene_name[x]] =+ 1,我猜这是一个错字,你真的orph_dict[gene_name[x]] += 1想增加计数器。

回答by Ella Sharakanski

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.

简而言之:gene_name[x]是一个可变对象,所以它不能被散列。要将对象用作字典中的键,python 需要使用其哈希值,这就是您收到错误的原因。

Further explanation:

进一步解释:

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

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

a = 5;
a = 3;

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

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

Mutable objects cannot be hashed. See this answer.

可变对象不能被散列。看到这个答案

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

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