For each row in the Text
column of my df
, I want to do the following:
Highlight the keywords
gross
,suck
,singing
&ponzi
Count the number of keywords in each row and store them in a
Count
column
import pandas as pd
data = {'Text': ['The bread tastes good','Tuna is gross','Teddy is a beach bum','Angela suck at singing!','oneCoin was a ponzi scheme'],
'ID': [1001,1002,1003,1004,1005]
}
df = pd.DataFrame(data, columns = ['ID', 'Text'])
print(df)
The desired output should include the Count
column and look like this :
My attempt (not the best! you can ignore this):
# keyword list
key_words = ['gross','suck','singing','ponzi']
# highlight the keywords
df['Text'].applymap(lambda x: "background-color: yellow" if x else "")
# count the keywords present in each row
df['Count'] = df['Text'].str.count(r"\b(?:{})\b".format("|".join(key_words)))
All attempts highly appreciated!