3

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 with bold colors; she does not...

I have a set of keywords:

keywords = ["of","is","she"]

How can I create a column for each keyword containing the number of occurrences of the keyword in each sentence of my dataframe? It would look something like:

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

Note: I look at how to count specific words from a pandas Series?, but it does not answer my question.

2 Answers 2

2

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  of  is  she
0                   Please wait outside of the house   1   0    0
1                    A glittering gem is not enough.   0   1    0
2  The memory we used to share is no longer coher...   0   1    0
3  She only paints with bold colors; she does not...   0   0    2
Sign up to request clarification or add additional context in comments.

Comments

0

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   1   0    0
1                    A glittering gem is not enough.   0   1    0
2  The memory we used to share is no longer coher...   0   1    0
3  She only paints with bold colors; she does not...   0   0    2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.