bikuser
bikuser

Reputation: 2093

how to merge two data frame with out adding duplicates in Python?

I have a dataframe looks like:

    x   y   z
1   3   7   1
2   7   5   2
3   8   2   3

and other dataframe looks like:

    x   y   z
1   7   5   4
2   8   2   8
3   4   5   5
4   7   3   7
5   9   5   3
6   3   7   9

Now I want to merge the two frames, and the frame after merge to be looked like this:

    x   y   z
1   3   7   1
2   7   5   2
3   8   2   3
4   4   5   5
5   7   3   7
6   9   5   3

Upvotes: 1

Views: 71

Answers (2)

jezrael
jezrael

Reputation: 862851

Use concat + drop_duplicates with define columns for check duplicates x and y + reset_index for remove duplicated index values:

df = pd.concat([df1,df2]).drop_duplicates(['x','y']).reset_index(drop=True)
print (df)
   x  y  z
0  3  7  1
1  7  5  2
2  8  2  3
3  4  5  5
4  7  3  7
5  9  5  3

Upvotes: 4

ILostMySpoon
ILostMySpoon

Reputation: 2409

Concatenate, then dedupe

pandas.concat([df1,df2]).drop_duplicates().reset_index(drop=True)

Upvotes: 2

Related Questions