Concatenate dataframe to a master dataframe only if the the values do not exist in the master dataframe

Question:

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?

Asked By: Hertyuw

||

Answers:

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

pd.concat([Df1, Df2[ ~Df2['Batch'].isin(Df1['Batch'])] ])
Answered By: Quang Hoang
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.