How can i search for a string from one column in a dataframe in a string value in another column in python?

Viewed 52

enter image description here I want to search 'search_value' in 'Search in string' column for every row and return TRUE if any of the words separated by | in search_value are present in 'Search in string'

Thanks in advance

1 Answers

You could make good use of the keyword any and use apply(lambda x:) to your dataframe. It would result in this:

df['Flag'] = df.apply(lambda x: True if any(i in x['Search in string'].split() for i in x['Search_value'].split('|')) else False,axis=1)

This results in the expected output:

                                        search_value  ...  Flag
0  civic|men|boy|furnishing|clothing|non durable|...  ...  True
1  environmental|cosmetic|beauty|perfume|apparel|...  ...  True
Related