Skip to content Skip to sidebar Skip to footer

Python Pandas Counting Matches Of Regex With Compound Words In A String

I have a dictionary of regular expressions and I want to count the matches in the dictionary with topics and regex that include compound words. import pandas as pd terms = {'ani

Solution 1:

Please see if this is what you were looking for:

import(re)
for k in terms.keys():
    df[k] = 0
    for words in re.sub("[()]","",terms[k]).split('|'):
        mask = df.Foo.str.contains(words, case = False)
        df[k] += mask
df


                                              Foo   Score   people  animals games
0   Superman was looking for a russian brown deer.      4        1        1     0
1   John adams started to play basket ball with ro...   6        3        0     1
2   Basketball or bball is a sport played by Steve...   2        1        0     2
3   The bald eagle flew pass the arctic fox three ...   7        0        3     0
4   The fox was sptted playing basket ball?             8        0        1     1

Post a Comment for "Python Pandas Counting Matches Of Regex With Compound Words In A String"