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