Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search DataFrame column for words in list

I am trying to create a new DataFrame column that contains words that match between a list of keywords and strings in a df column…

data = {
'Sandwich Opinions':['Roast beef is overrated','Toasted bread is always best','Hot sandwiches are better than cold']
}
df = pd.DataFrame(data)

keywords = ['bread', 'bologna', 'toast', 'sandwich']

df['Matches'] = [df.apply(lambda x: ' '.join([i for i in df['Sandwich iOpinions'].str.split() if i in keywords]), axis=1)

This seems like it should do the job but it’s getting stuck in endless processing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

for kw in keywords:
    df[kw] = np.where(df['Sandwich Opinions'].str.contains(kw), 1, 0)

def add_contain_row(row):
    contains = []
    for kw in keywords:
        if row[kw] == 1:
            contains.append(kw)
    return contains

df['contains'] = df.apply(add_contain_row, axis=1)

# if you want to drop the temp columns
[df.drop(columns=kw, inplace=True) for kw in keywords]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading