Creating new data frame by extracting cells (contains strings) with certain substring that matches with a list of words (have over 100)

Viewed 22

I already found a roundabout solution to the problem, and I am sure there is a simple way.

I got two data frames with one column in each. DF1 and DF2 contains strings.

Now, I try to match using .str contains in python, limited by knowledge, I am forced to manually enter substrings that I am looking for.

contain_values = df[df['month'].str.contains('Ju|Ma')]

This highlighted way is how I am able to solve the problem of matching substring within DF1 from DF2.

The current scenario pushes me to add 100 words using the vertical bar right here, str.contains('Ju|Ma').

Now can anyone kindly share some wisdom on how to link the second data frame that contain one column (contains 100+ words)

1 Answers

here is one way to do it. if you post a MRE, i would be able to test and share result, but the below should work

# create a list of words to search
w=['ju', 'ma']

# create a search string
s='|'.join(w)

# search using regex
import re
df[df['month'].str.contains(s, re.IGNORECASE)]
Related