Issue
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]})
Solution
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
Answered By - Boskosnitch
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.