I have 2 Pandas dfs, A and B. Both have 10 columns and the index \'ID\'. Where the IDs of A and B match, I want to replace the rows of B with the rows of A. I have tried to
You can empty your target cells in A (by setting them to NaN) and use the combine_first() method to fill those with B's values. Although it may sound counter-intuitive, this approach gives you the flexibility to both target rows and specific columns in 2 lines of code. Hope that helps.
An example replacing the full row's that have an index match:
# set-up
cols = ['c1','c2','c3']
A = pd.DataFrame(np.arange(9).reshape((3,3)), columns=cols)
B = pd.DataFrame(np.arange(10,16).reshape((2,3)), columns=cols)
#solution
A.loc[B.index] = np.nan
A = A.combine_first(B)
An example of only replacing certain target columns for row's that have an index match:
A.loc[B.index, ['c2','c3']] = np.nan
A = A.combine_first(B)