栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

熊猫drop_duplicates方法不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

熊猫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: 'xy', 3: 'xx', 4: 'yy'}, 'Y': {0: 'yy', 1: 'yy', 2: 'yx', 3: 'ix', 4: 'xi'}})#Drop directly causes the same errordf.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.loc[df.astype(str).drop_duplicates().index]Out[205]:   Keyword       X   Y0   apply  [1, 2]  yy2   apply      xy  yx3   terms      xx  ix4   terms      yy  xi#the list elements are still list in the final results.df.loc[df.astype(str).drop_duplicates().index].loc[0,'X']Out[207]: [1, 2]

编辑:用loc替换iloc。在这种特殊情况下,两者都作为索引与位置索引匹配而起作用,但这不是一般性的



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号