为什么 Python 告诉我 **类型错误:不可散列类型: 'list'** 当我有一个数据帧时?



我有以下数据帧和类似的第二个数据帧,我想进行比较。问题是我认为我混淆了数据类型:

df1 = pd.DataFrame(pd.read_csv("csv", delimiter=';', header=None, skiprows=1, names=['1', '2']))
df['1'].str.replace(r'[^ws]+', '')
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)

类型错误:不可更改的类型:'list'

在倒数第二行生成一系列列表。然后将该系列转换为一个集合。你不能这样做,因为集合的元素需要是可散列的,而列表不是(正如TypeError中所说的(。与列表相反,元组是可散列的。假设你的其余代码都能工作(我没有办法检查(,试试

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

相关内容

最新更新