DECROMAX

pandas isin using join to create regex for multiple values

Oct 13th, 2023 (edited)
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | Source Code | 0 0
  1. # pandas isin using join to create regex for multiple values
  2.  
  3. import pandas as pd
  4.  
  5. values = ['cat', 'dog', 'hamster']
  6.  
  7. # creates bool column that returns True if each element in the DataFrame is contained in values
  8. df['isin_example'] = (df['target_col'].str.contains('|'.join(values), case=False))
  9.  
  10. # Same example but with or 'pig' clause
  11. df['isin_example'] = (df['target_col'].str.contains('|'.join(words), case=False)) | (df['12_Work Location'].str.contains('pig', case=False))
Advertisement
Add Comment
Please, Sign In to add comment