简体   繁体   English

当我有 dataframe 时,为什么 Python 会告诉我 **TypeError: unhashable type: 'list'**?

[英]Why does Python tell me **TypeError: unhashable type: 'list'** when I have a dataframe?

I have the following dataframe and a similar second one which I want to compare.我有以下 dataframe 和我想比较的类似的第二个。 The problem is that I think I confuse datatypes:问题是我认为我混淆了数据类型:

df1 = pd.DataFrame(pd.read_csv("csv", delimiter=';', header=None, skiprows=1, names=['1', '2']))
df['1'].str.replace(r'[^\w\s]+', '')
df['1'] = df1['1'].str.replace('\d+', '')
df = df.apply(nltk.word_tokenize)
df = [nltk.word_tokenize(str(1)) for 1in df]
df = df.apply(lambda x: [item.lower() for item in x if item.lower() not in stop_words])
df = set(df)

TypeError: unhashable type: 'list'类型错误:不可散列类型:“列表”

On your second to last line you are generating a Series of lists.在倒数第二行,您正在生成一系列列表。 Then you are converting that series to a set.然后,您将该系列转换为一组。 You can't do that, because the elements of a set need to be hashable, and lists are not (as it says in the TypeError).你不能这样做,因为集合的元素需要是可散列的,而列表不是(正如它在 TypeError 中所说的那样)。 In contrast to lists, tuples are hashable.与列表相反,元组是可散列的。 Assuming that the rest of your code works (I have no way of checking), try假设您的代码的 rest 有效(我无法检查),请尝试

df = df.apply(lambda x: tuple(item.lower() for item in x if item.lower() not in stop_words))
df = set(df)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM