I have 2 large data frames, below 2 are just examples of how those 2 would look like.
df1 = pd.DataFrame(columns=['node', 'st1', 'st2'], data=[['a', 1, -1], ['b', 2, 2], ['c', 3, 4]])
node st1 st2
a 1 -1
b 2 2
c 3 4
df2 = pd.DataFrame(columns=['node', 'st1', 'st2'], data=[['a', 8, 5], ['b', 4, 6]])
node st1 st2
a 8 5
b 4 6
I want to update df1, st1 and st2, column values with the df2, st1 and st2, column values only if the node names in both data frames match. ALSO, if st1 or st2 columns values in df1 equals -1, do not update for that row and column i.e. keep it as -1. The result would look something like,
node st1 st2
a 8 -1
b 4 6
c 3 4
I've tried merging the 2 data frames using the basic pandas merge with left join which would give me a df with duplicate columns, then looping through each row in the resulting df to check the values of st1 and st2, and replace them only if it is not -1. But this would take a lot of time in larger data frames, which is why I would like to find the most effective way to do this.