Conditional Statement between two dataframes

61 Views Asked by At

I've scoured the forums here in search of an answer. I am trying to fill a column of df2 based on a condition in df1 and only if df1 and df2 code matches, then fill in df2 column X with df1 column X.

So example:

If df1['Type'] == 'X' & df1['code1'] == df2['code1'] return df1['Num']
Elif df1['Type'] == 'Y' & df1['code2'] == df2['code2'] return df1['Num']

df1 df1 example

df2 df2 example

df1 = df1.reset_index(drop=True)
df2 = df2.reset_index(drop=True)

df2.loc[(df2['code1'].isin(df1['code1'])) & (df1['Type'] == "X"), 'Num'] = df1['Num']
df2.loc[(df2['code2'].isin(df1['code2'])) & (df1['Type'] == "Y"), 'Num'] = df1['Num']
0

There are 0 best solutions below