Skip to content Skip to sidebar Skip to footer

Counting Specific Keywords In A Dataframe

I have a dataframe as such: A 0 Please wait outside of the house 1 A glittering gem is not enough. 2 The memory we used to share is no longer coher... 3 She only paints

Solution 1:

I assumed that you're looking for case-insensitive matches.

import pandas as pd
df = pd.DataFrame({
    'A': [
        'Please wait outside of the house',
        'A glittering gem is not enough.',
        'The memory we used to share is no longer coher...',
        'She only paints with bold colors; she does not...'
    ]
})
keywords = ["of","is","she"]
for keyword in keywords:
    df[keyword] = df['A'].apply(lambda _str: _str.lower().count(keyword))
print(df)

Output

                                                   A  ofis  she
0                   Please wait outside of the house   1001                    A glittering gem isnot enough.   0102  The memory we used to share is no longer coher...   0103  She only paints with bold colors; she does not...   002

Solution 2:

You can also do it this way:

df['is'] = df.A.str.count(r'is', flags=re.IGNORECASE)
df['of'] = df.A.str.count(r'of', flags=re.IGNORECASE)
df['she'] = df.A.str.count(r'she', flags=re.IGNORECASE)


                                                   A  of  is  she
0                   Please wait outside of the house   1001                    A glittering gem isnot enough.   0102  The memory we used to share is no longer coher...   0103  She only paints with bold colors; she does not...   002

Post a Comment for "Counting Specific Keywords In A Dataframe"