How to merge two pandas dataframes with condition

Question:

I have two dataframes like the following:

df1
      A       B 
0     0       3
1     0       2
2     1       5
3     1       3
4     2       5
5   'Ciao'  'log'
6     3       4


df2
      A   B 
0     0   -1
1     0   20
2     1   -2
3     1   33
4     2   17

I want to merge the two dataframes in order that the if A==0 keep the values of df1 and otherwise keep the values of df2.

At the end, I would like something like the follwing

df2
      A   B 
0     0   3
1     0   2
2     1   -2
3     1   33
4     2   17
Asked By: emax

||

Answers:

Assuming the dataframes are aligned (and that the duplicated index 3 in df1 is a typo), you do not want a merge but rather a conditional using where:

out = df1.where(df1['A'].eq(0), df2)

Output:

   A   B
0  0   3
1  0   2
2  1  -2
3  1  33
4  2  17

NB. if you really want a merge, you have to further explain the logic of the merge and provide a non-trivial example.

Updated example:

You seem to still have partially aligned indices, but want to get the intersection:

out = (df1.where(df1['A'].eq(0), df2)
          .loc[df1.index.intersection(df2.index)]
      )

Or:

out = (df1.reindex_like(df2)
          .where(df1['A'].eq(0), df2)
      )

output:

     A     B
0  0.0  -1.0
1  0.0  20.0
2  1.0  -2.0
3  1.0  33.0
4  2.0  17.0
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.