0

I have 2 dataframes that both has UserID column (df1 and df2)

I want to create a column in df1 where the value would be 1 if ID exists at least once and 0 else. The issue I'm running to is that since the UserID value is int, it will match a shorter ID as part of a longer ID and still return 1 (e.g. record for UserID: 123 will return 1 if df2 has UserID 12345) How can I create a new column with exact match?

This is the code I tried so far.

df1['Exists'] = [1 if x in df2['UserID'] else 0 for x in df1['UserID']]
ZickyM
  • 29
  • 3
  • Possible duplicate of [How to implement 'in' and 'not in' for Pandas dataframe](https://stackoverflow.com/questions/19960077/how-to-implement-in-and-not-in-for-pandas-dataframe) – Erfan Aug 02 '19 at 23:36
  • 2
    `df1['Exists'] = df2['UserID'].isin(df1['UserID']).astype(int)` – Erfan Aug 02 '19 at 23:37
  • That code helped! Thank you! – ZickyM Aug 03 '19 at 00:11

0 Answers0