我正在尝试比较两列sar_details_sent_norm_trigrams_和
熊猫数据框中的caap_details_sent_norm_trigrams_。也有其他的列,但是这些是我正在比较的两列。
我本质上是想保留两列文本值相同的记录。
我已经执行了几种方法,但是,我不断收到以下错误消息:
TypeError:无法散列的类型:“ set”
因此,我要么需要解决为什么我会收到此消息并加以解决,要么尝试其他方法。
任何建议将不胜感激。
谢谢。
程式码片段:
# Set with unique terms
df_sar['sar_details_sent_norm_trigrams_unique'] = df_sar['sar_details_sent_norm_trigrams_'].apply(lambda x: set([trigram for sent in x for trigram in sent]))
# Set with unique terms
df_caap['caap_details_sent_norm_trigrams_unique'] = df_caap['caap_details_sent_norm_trigrams_'].apply(lambda x: set([trigram for sent in x for trigram in sent]))
#Attempt 1:
df_caap[df_caap.caap_details_sent_norm_trigrams_unique.isin(df_sar.sar_details_sent_norm_trigrams_unique)]
#Attempt 2:
set(df_caap.caap_details_sent_norm_trigrams_unique).intersection(set(df_sar.sar_details_sent_norm_trigrams_unique))
TypeError跟踪(最近一次通话)
在()21
set(df1.columns).intersection(set(df2.columns))22
---> 23设置(df_caap.caap_details_sent_norm_trigrams_unique).intersection(set(df_sar.sar_details_sent_norm_trigrams_unique))
TypeError:无法散列的类型:“ set”
最佳答案
set
是可变数据类型,因此为它计算散列用作散列表数据结构(例如,dict,set)中的键是不安全的,因为如果数据结构发生突变,散列可能会更改,这会违反哈希表不变式。您可以改为使用不可变的frozenset
,并且可以将其用作哈希表中的键。
关于python - 如何解决错误消息:TypeError:不可哈希类型:'set',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56772296/