1

I have two dataframes. df1 and df2

import pandas as pd df1 = pd.DataFrame({ 'Buyer': ['Carl', 'Alex', 'Lauren'], 'Quantity': [18, 3, 8]}) df2 = pd.DataFrame({ 'Buyer': ['Carl', 'Alex', 'Maya', 'Emily'], 'Quantity': [18, 3, 5, 5]}) 

I was wondering if there was a way to compare df1 with df2 and append whatver is in not df1 to df2 so I will have an end result like

df2 = pd.DataFrame({ 'Buyer': ['Carl', 'Alex', 'Maya', 'Emily', 'Lauren'], 'Quantity': [18, 3, 5, 5, 8]}) 

3 Answers 3

5

You just need to concat the two dfs and then drop dupes

df2 = pd.concat([df1,df2]).drop_duplicates() >>> df2 Buyer Quantity 0 Carl 18 1 Alex 3 2 Lauren 8 2 Maya 5 3 Emily 5 
Sign up to request clarification or add additional context in comments.

Comments

3

Combine them like you said (append) and then just do a drop duplicate:

import pandas as pd df1 = pd.DataFrame({ 'Buyer': ['Carl', 'Alex', 'Lauren'], 'Quantity': [18, 3, 8]}) df2 = pd.DataFrame({ 'Buyer': ['Carl', 'Alex', 'Maya', 'Emily'], 'Quantity': [18, 3, 5, 5]}) df = df1.append(df2) df = df.drop_duplicates() print(df) 

output:

 Buyer Quantity 0 Carl 18 1 Alex 3 2 Maya 5 3 Emily 5 4 Lauren 8 

1 Comment

Its better to have one statement like df = df1.append(df2).drop_duplicates().
1

Using merge it can be achieved.

res = pd.merge(df2,df1,on=['Buyer','Quantity'],how='outer') 

output:

Buyer Quantity 0 Carl 18 1 Alex 3 2 Maya 5 3 Emily 5 4 Lauren 8 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.