简体   繁体   中英

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? The column not matching being 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM