How to make pandas str.contains faster

Viewed 238

How can I make python loop faster through DataFrame columns with 1 million rows and search for a pattern of strings? Should return True or False

pattern_example = "home|property|house|apartment"

This is what I have right now

df[field].str.contains(pattern_example.lower(), case = False, regex=False)

This is what I am trying to implement

df[field].apply(lambda x: True if pattern_example.lower() in x else False)

However, it cannot recognize the OR(|) operator and searchers for full "home|property|house|apartment"

Any suggestions?

1 Answers

@furas thanks for the contribution. It worked.

This is what I used

df[field].apply(lambda x: True if any (word in x for word in pattern.lower().split('|')) else False)

Related