Home > Back-end >  Python: how to merge two pandas dataframes with condition
Python: how to merge two pandas dataframes with condition

Time:11-22

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

CodePudding user response:

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

CodePudding user response:

Use panda update. This uses indices and may not always work

df2.update(df1.query("A==0"))
df2
  • Related