我从json中读取数据,其中一些是重复的所以我想删除它们,请注意有2列(douban_info和omdb_info ) 仍然是 json/dict 格式
但是,如果我执行 pd_data.drop_duplicates(['douban_info']) (json 内容的列),它将失败。
但如果我执行 pd_data.drop_duplicates(['detail_url'])(常规列),它会起作用。
那么我怎样才能成功删除这些重复项呢??
异常:
TypeError Traceback (most recent call last)
<ipython-input-13-a0091f87b553> in <module>()
1 pd_data.drop_duplicates(['detail_url']) # this works
----> 2 pd_data.drop_duplicates(['douban_info']) # this failed
3 # pd_data2.describe()
...
TypeError: unhashable type: 'dict'
注意:我可以把数据文件放在哪里?所以你可以试试看?
请您参考如下方法:
TypeError: unhashable type: 'dict' 表示您在尝试用于 drop_duplicates 的列中有一个字典。
drop_duplicates 需要能够将列中的值相互比较,它通过散列来实现,您不能将字典转换为散列。
因为只有当两个值相等时才能确定一行是否重复。
您需要做的是将此字典更改为可哈希的内容。也许是一个字符串。
pd_data['douban_info_string'] = pd_data['douban_info'].astype(str)
pd_data.drop_duplicates('douban_info_string')
应该可以。
效率不高或不漂亮,但应该可以。
