删除 Pandas 中的重复项,不包括一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38445416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:36:31  来源:igfitidea点击:

Dropping duplicates in Pandas excluding one column

pythonpandas

提问by Jstuff

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

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

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

The result would be

结果是

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

I found online that drop_dupilcateswith the subsetparameter could work, but I am unsure of how I can apply it to multiple columns.

我在网上发现drop_dupilcates使用该subset参数可以工作,但我不确定如何将其应用于多列。

回答by ayhan

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']))