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

Pandas drop only duplicates matching a condition

I have a dataframe with duplicates in "Model Number". I want to keep each row that has "Difference" equaling 0.00 and remove its duplicate, but if a duplicate pair does not have a "Difference" equaling 0.00 then I don’t want to remove it.
Original Dataframe

Desired Dataframe

Thank you for your help.

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

>Solution :

Try the following code;

df1 = df[df["Difference"] == 0]
lst_model = [i for i in df["Model Number"].unique() if i not in df1["Model Number"].unique()]
df2 = df[df["Model Number"].isin(lst_model)]
df_final = pd.concat([df1,df2])
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