Home > Blockchain >  Using str.contains instead of .isin with pandas
Using str.contains instead of .isin with pandas

Time:10-14

If my goal is to see if any values in one dataframe's column match in another dataframe's column I can use .isin like so:

df1 = pd.DataFrame({'name': ['Marc', 'Jake', 'Sam', 'Brad']})
df2 = pd.DataFrame({'IDs': ['Jake', 'John', 'Marc', 'Tony', 'Bob']})

print(df1.assign(In_df2=df1.name.isin(df2.IDs).astype(int)))

Output:
   name  In_df2
0  Marc       1
1  Jake       1
2   Sam       0
3  Brad       0 

However if I don't want an exact match and want to avoid looping is there a way to substitute .isin with str.contains()? Something like this?

print(df1.assign(In_df2=df1.name.str.contains(df2.IDs).astype(int)))

right now this returns:

TypeError: unhashable type: 'Series'

Thanks!

CodePudding user response:

Use a regex like this:

pattern = fr"(?:{'|'.join(df2['IDs'])})"

df1['In_df2'] = df1['name'].str.contains(pattern).astype(int)

Output:

>>> df1
        name  In_df2
0       Marc       1
1       Jake       1
2        Sam       0
3       Brad       0

>>> pattern
'(?:Jake|John|Marc|Tony|Bob)'
  • Related