python的drop duplicates_python – Pandas drop_duplicates方法不起作用

python的drop duplicates_python – Pandas drop_duplicates方法不起作用drop_duplicates不能用于数据框中的列表,如错误消息所示.但是,您可以删除作为str的数据帧上的重复项,然后使用结果中的索引从原始df中提取行.建立df=pd.DataFrame({‘Keyword’:{0:’apply’,1:’apply’,2:’apply’,3:’terms’,4:’terms’},’X’:{0:[1,2],1:[1,2],2…

drop_duplicates不能用于数据框中的列表,如错误消息所示.但是,您可以删除作为str的数据帧上的重复项,然后使用结果中的索引从原始df中提取行.

建立

df = pd.DataFrame({‘Keyword’: {0: ‘apply’, 1: ‘apply’, 2: ‘apply’, 3: ‘terms’, 4: ‘terms’},

‘X’: {0: [1, 2], 1: [1, 2], 2: ‘xy’, 3: ‘xx’, 4: ‘yy’},

‘Y’: {0: ‘yy’, 1: ‘yy’, 2: ‘yx’, 3: ‘ix’, 4: ‘xi’}})

#Drop directly causes the same error

df.drop_duplicates()

Traceback (most recent call last):

TypeError: unhashable type: ‘list’

#convert hte df to str type, drop duplicates and then select the rows from original df.

df.iloc[df.astype(str).drop_duplicates().index]

Out[205]:

Keyword X Y

0 apply [1, 2] yy

2 apply xy yx

3 terms xx ix

4 terms yy xi

#the list elements are still list in the final results.

df.iloc[df.astype(str).drop_duplicates().index].loc[0,’X’]

Out[207]: [1, 2]

今天的文章python的drop duplicates_python – Pandas drop_duplicates方法不起作用分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/11800.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注