Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicates using column value with some ignore condition

I have two columns in my excel file and I want to remove duplicates from ‘A’ column with an ignore condition. The columns are as follow:

A B
1 10
1 20
2 30
2 40
3 10
3 20

Now, I want it to turn into this:

A B
1 10
2 30
2 40
3 10

So, basically I want to remove all duplicates except when column ‘A’ has value 2 (I want to ignore 2). My current code is as follows but it does not work for me as it removes duplicates with value ‘2’ too.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df = pd.read_excel(save_filename)
df2 = df.drop_duplicates(subset=["A", "B"], keep='first')
df2.to_excel(save_filename, index=False)

>Solution :

You can use two conditions:

df[~df.duplicated(subset="A") | df["A"].eq(2)]

   A   B
0  1  10
2  2  30
3  2  40
4  3  10
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading