Home > Software engineering >  Concatenate dataframe to a master dataframe only if the the values do not exist in the master datafr
Concatenate dataframe to a master dataframe only if the the values do not exist in the master datafr

Time:09-18

Let's say I have 2 dataframes:

Df1 = 

  Batch  Result
0     A       3
1     A       5
2     B       5
3     B       6
4     C       8
5     C       3


Df2=

  Batch  Result
0     C       8
1     C       3
2     D       6
3     D       1

I want to concat Df2 to Df1, but I only want to have batch D from Df2 in Df1. The output should be look like this:

Df1 = 

  Batch  Result
0     A       3
1     A       5
2     B       5
3     B       6
4     C       8
5     C       3
2     D       6
3     D       1

How can I do this with Pandas?

CodePudding user response:

You can remove the batch that are in Df1 from Df2 before concat:

pd.concat([Df1, Df2[ ~Df2['Batch'].isin(Df1['Batch'])] ])
  • Related