Home > Back-end >  How to compare two columns in two different pandas dataframes?
How to compare two columns in two different pandas dataframes?

Time:09-22

I am running a selenium script which captures the office location and employee name and saves to a dataframe.

Employee   Office
John Doe   Building One
Kim Joe    Building One
Harry P   Building two
Harry P   Building Three

CSV
   Employee   Office
    Kim Joe   Building One
    Harry P   Building two
    Harry P   Building Three

my code is below

df2 = df2.append({'Employee': emp.text,'Office': loc}, ignore_index=True)

I am reading the csv file into dataframe df1 and trying to compare the two columns in two dataframes, I tried the below code but it wont work because the indexes might be different

df2[df1.ne(self.df2).any(axis=1)]

What I am trying to do is want to get the employee:office which exist in df2 but not in df1, I am not sure how merge works, tried but didnt get the desired result, also tried using dictionary instead of dataframes but I guess it didnt work because of non unique keys, also open to other ways

Output
John Doe   Building One

CodePudding user response:

Try using np.isin:

>>> df2[~np.isin(df2, df).all(axis=1)]
   Employee        Office
0  John Doe  Building One
>>> 
  • Related