'How to find multiple keywords in a string column in python

I have a column

|ABC|
-----
|JWUFT_P_RECF_1_DK1_VWAP_DFGDG_P_REGB_1_PK1_XYZ|

i WANT TO CHECK IF THERE ARE WORDS "DK" AND 'PK' in the row or not. i need to perform this with different words in entire column.

match = ['DK', 'PK']

i used df.ABC.str.split('_').isin(match), but it splits into list but getting error

SystemError: <built-in method view of numpy.ndarray object at 0x0000021171056DB0> returned a result with an error set

What is the best way to get the expected output, which is a bool True|False

Thanks.



Solution 1:[1]

You can use python re library to search a string:

import re
s = "JWUFT_P_RECF_1_DK1_VWAP_DFGDG_P_REGB_1_PK1_XYZ"
r = re.search(r"(DK).*(PK)|(PK).*(DK)",s) # the pipe is used like "or" keyword

If what your parameters are matched with the string it will evaluate to True:

if r:
    print("hello world!")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahmud Alptekin