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

Delete row indices based on common columns in a Dataframe

I have following two dataframes df1 and df2

  final  raw  st
   abc   12  10
   abc   17  15
   abc   14  17

and

   final   raw
    abc   12
    abc   14 

My expected output is

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

  final  raw  st
   abc   17  15

I would like to delete rows based on common column value.
My try:
df1.isin(df2)
This is giving me Boolean result. Another thing, I tried
df3 = pd.merge(df1, df2, on = ['final', 'raw'], how = 'inner') so that we get all the common columns for df1 and df3.

>Solution :

You are closed with merge you just need extra step. First you need to perform an outer join to keep all rows from both dataframes and enable indicator of merge then filter on this indicator to keep right values (from df2). Finally, keep only columns from df1:

df3 = pd.merge(df1, df2, on = ['final', 'raw'], how='outer', indicator=True) \
        .query("_merge == 'left_only'")[df1.columns]
print(df3)

# Output
  final  raw  st
1   abc   17  15
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