pandas drop_duplicates无法散列的类型:“ numpy.ndarray”,“ set”和“ list” [英] pandas drop_duplicates unhashable type: 'numpy.ndarray', 'set' and 'list'

查看:117
本文介绍了pandas drop_duplicates无法散列的类型:“ numpy.ndarray”,“ set”和“ list”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在数据框

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

结果 dataframe 应该看起来像

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

我尝试了 df.drop_duplicates('A',inplace = True),但是出错了,

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

unhashable type: 'numpy.ndarray'

我还使用 df将 A 转换为列表和集合[ 'A']。apply(列表) df ['A']。apply(集合),然后使用 drop_duplicates ,但全部失败,并带有不可散列的类型: set和 list 。我想知道如何解决该问题。

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.

推荐答案

您需要元组

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

这篇关于pandas drop_duplicates无法散列的类型:“ numpy.ndarray”,“ set”和“ list”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆