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 dataframe delete duplicate base date column

I have 2 datafames with same columns that one of the column is date.

I try to concat the dataframes and delete the row with the earlier date, when the primary keys are same.

Input (df1 & df2):

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

pk1 | pk2 |  C  |   DATE  
 1  |  2  |  3  | 05-09-22
 2  |  3  |  4  | 05-09-22


pk1 | pk2 |  C  |   DATE  
 1  |  2  |  5  | 06-09-22

Output:

pk1 | pk2 |  C  |   DATE  
 2  |  3  |  4  | 05-09-22
 1  |  2  |  5  | 06-09-22

>Solution :

You need to drop_duplicates while keeping the first.

df = pd.concat([df1,df2]) # concating
df.sort_values(by=['DATE'], ascending=True, inplace=True) # sorting by date
df = df.drop_duplicates(subset=['pk1', pk2], keep='first') # dropping duplicates
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