0
votes

I'm struggling with the following problem:

I have two dataframes: df1

 A      B      C
 1      5      8
 2      1      2
 3      2      1
 4      3      6

and df2 with same column names, but not as much columns as df1:

 A      B      
 1      1      
 8      2      
 1      5      
 6      3

df1 and df2 always have the same amount of rows, only the amount of columns of df2 is less or equal than df1. Also, the column names are the same, but not the values in the column (they can be the same, but this is definitely not always the case)

Now, I want to create a new dataframe, where the overlapping columns between df1 and df2 (column A and B, NOT C) are determined by df2, but has the same shape as df1 (so df1 is dominating in amount of columns, but df2 is dominating in which value to take from the overlapping columns). Important to know is that I don't want to specify which columns are overlapping.

So the result should give:

df3:

 A      B      C
 1      1      8
 8      2      2
 1      5      1
 6      3      6

Is this possible, especially with the difficulty of not specifying upfront the overlapping columns. anyone a clever solution? Because it seems not to be possible with all the possible variations of merge and join.

1

1 Answers

1
votes

As long as there are no column labels in df2 not present in df1, you can use

df3 = df1.copy()
df3.loc[:,df2.columns] = df2