[英]Dropping duplicates in Pandas excluding one column
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?如何消除与 5 列中的 4 列匹配的重复项? The column not matching being Description
.不匹配的列是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_duplicates
with the subset
parameter could work, but I am unsure of how I can apply it to multiple columns.我在网上发现带有subset
参数的drop_duplicates
可以工作,但我不确定如何将其应用于多列。
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']))
如何添加不同的多列,如 id 和 description?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.