pandas drop_duplicates unhashable type: 'numpy.ndarray', 'set' and 'list'

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

I am trying to use drop_duplicates on a column of a dataframe,

A          len ['1', '2'] 2 ['1', '2'] 2 ['3']      1 ['4', '5'] 2  ['4', '5'] 2 

The result dataframe should look like

A          len ['1', '2'] 2 ['3']      1 ['4', '5'] 2  

I have tried df.drop_duplicates('A', inplace=True), but got error,

unhashable type: 'numpy.ndarray' 

I have also converted A to lists and sets using df['A'].apply(list) and df['A'].apply(set), and then using drop_duplicates, but all failed with unhashable type: 'set' and 'list'. I am wondering how to resolve the issue.

回答1:

You need tuple:

df['A'].apply(tuple) 

So use duplicated with boolean indexing:

df = df[~df['A'].apply(tuple).duplicated()] print (df)         A  len 0  [1, 2]    2 2     [3]    1 3  [4, 5]    2 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!