You can easily use numpy.where
. And i think it should work best in this case too.
Let's say you have the following DataFrames
import pandas as pd
df1=pd.DataFrame({'X':[1,3,4,6,5],
'X1':[2,3,4,6,3],
'Y1':[4,2,1,51,3],
'Z1':[2,3,4,1,5]})
df2=pd.DataFrame({'L':[2,3,4,1,4],
'X2':[2,3,4,6,5],
'Y2':[4,3,4,6,3],
'Z2':[2,2,1,51,3]})
And you want to change the value of X based on the conditions if X1==X2 & Y1==Y2 & Z1==Z2
. Also lets say the value you want to update is from column L in this case.
You can use numpy.where
like this
df1['X']=np.where((df1['X1']==df2['X2'])&(df1['Y1']==df2['Y2'])&(df1['Z1']==df2['Z2']),df2['L'],,df1['X'])
It would only change the first row as the conditions only gets satisfied there. This function is changing the values to df2['L']
if it meets the condition and keeping the original values if the conditions are not met.
Read more about np.where
Update: The dataframes in the question are not equal. It doesn't matter if they don't have equal columns but the rows should be equal for the sake of comparison. Below is the example in which the two data frames are not equal and how numpy.where
is performed in that case.
import pandas as pd
import numpy as np
df1=pd.DataFrame({'X':[1,3,4,6,5],
'X1':[2,3,4,6,3],
'Y1':[4,3,1,51,3],
'Z1':[2,3,4,1,5]})
df2=pd.DataFrame({'L':[2,3,4,1,4,5,1],
'X2':[2,3,4,6,5,2,3],
'Y2':[4,3,4,6,3,8,7],
'Z2':[2,3,1,51,3,9,9],
'R2':[2,5,1,2,7,3,9]})
#make both the dataframes equal
for i in range(len(df2)-len(df1)):
df1=df1.append(pd.Series(),ignore_index=True)
df1['X']=np.where((df1['X1']==df2['X2'])&(df1['Y1']==df2['Y2'])&(df1['Z1']==df2['Z2']),df2['L'],df1['X'])
#drop those null values which have been appended above to get back to original
df1=df1.dropna(how='all')