用字典删除重复的行

cqc*_*991 1 python json pandas

我从读取数据json,其中一些是重复数据,因此我想删除它们,请注意,还有2列(douban_infoomdb_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'
Run Code Online (Sandbox Code Playgroud)

注意:我可以放在哪里data file?所以你可以尝试一下?

fir*_*ynx 6

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')
Run Code Online (Sandbox Code Playgroud)

应该管用。

不是很有效或漂亮,但应该可以。