Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping duplicates in Pandas excluding one column

Tags:

python

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 

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.

like image 272
Jstuff Avatar asked Jul 18 '16 20:07

Jstuff


People also ask

How do you remove duplicates from a DataFrame in Python based on column?

To drop duplicate columns from pandas DataFrame use df. T. drop_duplicates(). T , this removes all columns that have the same data regardless of column names.

How do I remove duplicates from one column?

To remove duplicates of only one or a subset of columns, specify subset as the individual column or list of columns that should be unique. To do this conditional on a different column's value, you can sort_values(colname) and specify keep equals either first or last .

Does drop duplicates ignore index?

Return DataFrame with duplicate rows removed. Considering certain columns is optional. Indexes, including time indexes are ignored.


1 Answers

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'])) 
like image 155
ayhan Avatar answered Sep 21 '22 02:09

ayhan