df1 for example is
| col1 | col2 | col3 |
|---|---|---|
| abcdef | ghijkl | mnopqr |
| abcdef1 | ghijkl1 | mnopqr1 |
df2 is
| col1 |
|---|
| ghijkl1 |
essentially I want to add a col4 to df1 with the value "MM" if the value in df1col2 appears in df2col1
the final df1 would be:
| col1 | col2 | col3 | col4 |
|---|---|---|---|
| abcdef | ghijkl | mnopqr | |
| abcdef1 | ghijkl1 | mnopqr1 | MM |
>Solution :
Use Series.isin and then chain .map to convert True to ‘MM’, and False to a NaN value.
df1['col4'] = df1['col2'].isin(df2['col1']).map({True:'MM',False:np.nan})
print(df1)
col1 col2 col3 col4
0 abcdef ghijkl mnopqr NaN
1 abcdef1 ghijkl1 mnopqr1 MM