Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading