-1

I have two dataframes table1 and table2:

table1

table1

table2

table2

I want to compare table1 with table2 and return miss rows from each other of table 1 and table 2.

  • 2
    add some example to show your expactations – Ali Nov 17 '19 at 12:15
  • 2
    It seems like it was already answered [here](https://stackoverflow.com/questions/28901683/pandas-get-rows-which-are-not-in-other-dataframe) – YonGU Nov 17 '19 at 12:18
  • Where are you at that you're stuck? Have you got further than screenshotting some intput - eg have you got those two tables into two dataframes already and tried something that doesn't work? – Jon Clements Nov 17 '19 at 12:19
  • `df1.loc[~df1[col].isn(df2[col])]` – Umar.H Nov 17 '19 at 12:29
  • 1
    use: `df1.merge(df2,how='outer',indicator=True).query('_merge != "both"').drop('_merge',axis=1)` – ansev Nov 17 '19 at 12:33

2 Answers2

1

You can do:

only_in_1 = table1.merge(table2.assign(key=1), how='left')
only_in_1 = only_in_1[pd.isna(only_in_1['key'])].drop('key', axis=1)
Aryerez
  • 3,417
  • 2
  • 9
  • 17
0
import pandas as pd
import numpy as np
df1 = table1
df2 = table2

df_inner = df1.merge(df2, on = ['A', 'B', 'C'], how = 'inner')
df_outer = df1.merge(df2, on = ['A', 'B', 'C'], how = 'outer')
df_inner['label'] = np.ones(len(df_inner))
df = df_outer.merge(df_inner, on = ['A', 'B', 'C'], how = 'outer')
df = df[df['label'].isna()]
df = df.drop(['label'], axis = 1)
wa007
  • 105
  • 8