1

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

  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.

Manglu
  • 258
  • 2
  • 10

2 Answers2

0

You need to refer to the correct column when using isin.

result = df1[~df1['raw'].isin(df2['raw'])]
timgeb
  • 76,762
  • 20
  • 123
  • 145
0

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
Corralien
  • 109,409
  • 8
  • 28
  • 52