johnnyb johnnyb - 1 month ago 7
Python Question

I would like to know how to iterate through df.column3 find match in df.column2 and add name of df.column1 based on matches to a new column df.column4

Test data looks like:


Column1 Column2 Column3
johnny 100 900
matty 300 100
grapy 400 300
snapp 500 300


Expected Result:


Column1 Column2 Column3 Column4
johnny 100 900 None
matty 300 100 johnny
grapy 400 300 matty
snapp 500 300 matty


Attempted to run """df['Column1'][df['Column2'].isin(df['Column3'])"""
This partly worked but failed on some of the items that didnt match
Thanks for your help

Answer

You could do this using a dictionnary of possible values :

df = pd.DataFrame([['matty', 300, 900], ['grapy', 400, 300], ['snapp', 500, 300]], columns=['Column1', 'Column2', 'Column3'])
df = df.set_index('Column2')

dic = df['Column1'].to_dict()
df['Column4'] = [dic[n] if n in dic.keys() else None for n in df['Column3']]
df = df.reset_index()

df
Out[114]: 
   Column2 Column1  Column3 Column4
0      300   matty      900    None
1      400   grapy      300   matty
2      500   snapp      300   matty
Comments