Reputation: 89
I need to a result column in df1, when
Condition 1 : Values of each row in Column1 of df1 match with that of Column1 of df2 (if its true it should move to condition2) e.g A1 of df1 should match with A1 of df2 and similarly for A2 and so on.
Condition 2 : If the first condition is true it should also match second column. B2(df1) should be equal to B2(df2).
The result column is in df1. and return "match" or "do not match" in respective row when both conditions are true.
I have tried the below code but it matches the whole column instead of row by row. (I assume)
import pandas as pd
#reading data
df1 = pd.read_excel("Book1.xlsx")
df2 = pd.read_excel("Book2.xlsx")
c1 = df1['EFE'] == df2['EFE'] #EFE IS COLUMN 1
c2 = df1['DOR'] == df2['DOR'] #DOR IS COLUMN 2
#Giving No error but result is not as desired. Each EFE is marked not good
if c1 is True and c2 is True:
df1['Result'] = "Good"
else:
df1['Result'] = "Not Good"
Code is edited. to update the latest changes.
Upvotes: 1
Views: 49
Reputation: 111
Try this:
df1['is_good'] = ((df1['EFE'] == df2['EFE']) & (df1['DOR'] == df2['DOR']))
In your example you first assign "Good" to the whole "Result" column and then "Not Good", which I guess you don't really want.
Upvotes: 1