Home > Software engineering >  Create new col4 in df1 with a value1, if the single column in df2 contains the value2 from a column
Create new col4 in df1 with a value1, if the single column in df2 contains the value2 from a column

Time:09-15

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

CodePudding user response:

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
  • Related