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

How to move ALL duplicated rows into separate dataframe

My code is removing all duplicates using the drop_duplicates, keep=false.

The issue I’m having is that before I remove the duplicates I want to move all removed duplicates to a separate dataframe. I’ve come up with the below line of code, however I think its leaving one duplicate remaining and not removing ALL duplicates.

duplicates_df = combined_df.loc[combined_df.duplicated(subset='Unique_ID_Count'), :]

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

combined_df.drop_duplicates(subset='Unique_ID_Count', inplace=True, keep=False)

Do you have any ideas on how I can move all duplicates dropped in the second line of code to the duplicates_df dataframe?

Any help would be much appreciated, thanks!

>Solution :

Try this:

duplicates_df = combined_df.loc[combined_df.duplicated(subset='Unique_ID_Count', keep=False)]
combined_df   = combined_df.loc[~combined_df.duplicated(subset='Unique_ID_Count', keep=False)]
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