I have two dataframes. If the values in the index exist in both dataframes then I need to update the NaN values in the first one with the integer value in the second, but only if the value in the second is equal to 1. Below is any example of what I'm trying to do.
df1
index A B C
aaa NaN 1 NaN
bbb NaN NaN 10
ccc 3 NaN 6
ddd NaN NaN NaN
eee NaN NaN NaN
df2
index A B C
aaa 1 1 NaN
bbb NaN NaN 10
eee NaN 1 NaN
df1 (after the procedure)
index A B C
aaa *1* 1 NaN
bbb NaN NaN 10
ccc 3 NaN 6
ddd NaN NaN NaN
eee NaN *1* NaN
The values in df1 (after the procedure) with asterisks around them are the only ones that should change. All other values in df1 (after the procedure) should remain untouched. This is an extension of a previous question I asked and while the answer I got works great I can't seem to extend it to accomplish this last task. I'm still trying to fully understand how it works. I'm new to python. For quick refrence this is the code I have so far that I am trying to extend. Thanks for any help.
df1 = df1.set_index('values')
df2 = df2.set_index('values')
cols = [*df1.columns]
for col in cols:
#Update to df1
df1[col].update(df2.loc[df2[col].isnull(), col].fillna('-'))
df1[col].replace('-', np.NaN, inplace = True)
#Update to df2, sum if they both have numbers
df2[col].update(df2.loc[~df2[col].isnull(), col] + df1.loc[~df1[col].isnull(), col])