Suppose I have one DataFrame:
df1 = pd.DataFrame(data=[['AB',20,'John'],['BC',18,'Bill'],['BD',22,'Mos']],columns=['ID','Age','Name'])
df1
ID Age Name
0 AB 20 John
1 BC 18 Bill
2 BD 22 Mos
and I want to add it a Weight
column contained in a second frame by comparing their common column (ID
field in this case):
df2 = pd.DataFrame(data=[['BD',80],['AA',72],['AB',82]],columns=['ID','Weight'])
df2
ID Weight
0 BD 80
1 AA 72
2 AB 82
if there isn't any match then the value should be None
so the output should be:
ID Age Name Weight
0 AB 20 John 82.0
1 BC 18 Bill NaN
2 BD 22 Mos 80.0
Any way of doing it besides brute force iterating on df1
'?
If the isin()
function will also return an index of the occurrence and not just Boolean value then it would be easy but I can't find this option.