You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

筛选含特定单词的元组行报错:unhashable type: 'Int64Index'

解决DataFrame筛选含特定单词行时的"unhashable type: 'Int64Index'"错误

错误原因分析

你的代码存在两个问题:

  1. 调用loc时用了圆括号data.loc(...),正确写法应为方括号data.loc[...]
  2. s.index[s]返回的是重复的原索引集合(Int64Index类型),直接传给loc会因该类型不可哈希触发报错

修正方案

方案一:修复原代码逻辑

通过分组聚合判断每行是否包含目标单词:

s = data['stemming'].explode().isin(['accord'])
# 按原索引分组,只要该行有一个元素匹配就标记为True
mask = s.groupby(level=0).any()
data_new = data.loc[mask]

方案二:更简洁的直接判断法

无需explode,直接检查每个列表/元组是否包含目标单词:

data_new = data[data['stemming'].apply(lambda x: 'accord' in x)]

验证结果

两种方法均可得到你期望的DataFrame:

idstemming
2[accord, yes]
3[accord]
4[maybe, do, accord, not]

内容的提问来源于stack exchange,提问作者Dewani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 07:25:19