check frequency of keyword in df in a text

I have a given text string:
text = """Alice has two apples and bananas. Apples are very healty."""

and a dataframe:

word
apples
bananas
company

I would like to add a column "frequency" which will count occurrences of each word in column "word" in the text.

So the output should be as below:

word frequency
apples 2
bananas 1
company 0

>Solution :

import pandas as pd
df = pd.DataFrame(['apples', 'bananas', 'company'], columns=['word'])
para = "Alice has two apples and bananas. Apples are very healty.".lower()
df['frequency'] = df['word'].apply(lambda x : para.count(x.lower()))

    word    frequency
0   apples  2
1   bananas 1
2   company 0

Leave a Reply