-2

I have 2 data frames as below:

df1(main data)
UID    SG
1      A
2      B
3      C
4      D
5      E

df2
UID    AN    SG
1      x      A
3      y      C
2      z      B
1      xy     A
3      v      C

Now, I want to add a new column to df1, say "isPresent". This column will have "Yes" if the UID from df1 is present in df2 and "No" if UID in not in df2. So my df1 will finally looks like this,

df1
UID    SG    isPresent
1      A      Yes
2      B      Yes
3      C      Yes
4      D      No
5      E      No

My approach is taking an intersection of the UIDs from both the dataframes and then a for loop in df1 to add data cell by cell.

But I want to apply an approach without using for loop and using pandas as much as possible, if possible.

Shivam
  • 1
  • 2

1 Answers1

0

You can try this:

import pandas as pd
df1 = pd.DataFrame({'UID':[1, 2, 3, 4, 5], 'SG':['A', 'B', 'C', 'D', 'E']})
df2 = pd.DataFrame({'UID':[1, 3, 2, 1, 3], 'AN':['x', 'y', 'z', 'xy', 'v'], 'SG':['A', 'C', 'B', 'A', 'C']})
df1['isPresent'] = df1['UID'].isin(df2['UID'])

Alternatively, try this:

df1['isPresent'] = df1.UID.isin(df2.UID)

output:

  SG  UID  isPresent
0  A    1       True
1  B    2       True
2  C    3       True
3  D    4      False
4  E    5      False
Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43