Search code examples
pythonpandasgeneric-programming

remove specific rows based on comparing 2 rows if not a match in python


I have 2 dataframes with 2 columns each, 1 column of both dataframes has the same values but not some of them does not match

df_1                     df_2
ID_1     ID_2            ID_1    ID_2
A_123_t  HS123           HS123   IG098
A_234_t  HS234           HS234   IG987
A_345_t  HS345           HS345   IG876
A_456_t  HS456           HS643   IG765
A_567_t  HS567           HS567   IG654
A_252_t  HS678           HS765   IG543
A_276_t  HS789           HS798   IG432
A_986_t  HS890           HS890   IG321
A_243_t  HS734           
A_926_t  HS273

I want to remove the rows which does not match and the rows which the other dataframe does not have.

Is there a generic way to approach this in python?

Guys you will be my heroes!

Cheers


Solution

  • Try this:

    df_1[df_1['ID_2'].isin(df_2.ID_1)]
    

    Read this for reference.