在Pandas中删除重复项(不包括一列) [英] Dropping duplicates in Pandas excluding one column

查看:83
本文介绍了在Pandas中删除重复项(不包括一列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但是我无法在互联网上找到任何信息

This seems simple, but I can not find any information on it on the internet

我有一个像下面这样的数据框

I have a dataframe like below

City    State Zip           Date        Description       
Earlham IA    50072-1036    2014-10-10  Postmarket Assurance: Devices
Earlham IA    50072-1036    2014-10-10  Compliance: Devices
Madrid  IA    50156-1748    2014-09-10  Drug Quality Assurance

如何消除与5列中的4列匹配的重复项?该列不匹配为Description.

How can I eliminate duplicates that match 4 of 5 columns? The column not matching being Description.

结果将是

City    State Zip           Date        Description       
Earlham IA    50072-1036    2014-10-10  Postmarket Assurance: Devices
Madrid  IA    50156-1748    2014-09-10  Drug Quality Assurance

我在线发现具有subset参数的drop_dupilcates可以工作,但是我不确定如何将其应用于多个列.

I found online that drop_dupilcates with the subset parameter could work, but I am unsure of how I can apply it to multiple columns.

推荐答案

您实际上已经找到了解决方案.对于多列,子集将是一个列表.

You've actually found the solution. For multiple columns, subset will be a list.

df.drop_duplicates(subset=['City', 'State', 'Zip', 'Date']) 

或者,只需说明要忽略的列即可:

Or, just by stating the column to be ignored:

df.drop_duplicates(subset=df.columns.difference(['Description']))

这篇关于在Pandas中删除重复项(不包括一列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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