I have an example dataframe:
df = pd.DataFrame({'Reference':[1,2,3,4,5],
'Description':["foo", "bar", "green bond house", "supply dog chain", "climate finance house"]})
And a list of keywords that I am interested in knowing how often they appear in each row of the dataframe:
wordlist = ["green finance", "green bond", "green credit", "green financing", "climate finance"]
The following code does this and works perfectly:
df['total'] = df['Description'].str.count(r'\b|\b'.join(wordlist))
With the following output:
However. My keywords are not always simple strings, sometimes they can be like this:
- "climate finance" AND NOT "house"
- NEAR((supply, chain), 2) i.e. where the word 'supply' appears within 2 words of 'chain'
Is this type of str.count possible in Python? And if not, does anyone have any advice on how to solve this problem?
Many thanks

