Skip to content Skip to sidebar Skip to footer

Using Str.findall To Retrieve The Exact Match From Dictionary

I have the following dictionary dictionary = {'car':1234, 'light-blue':112, 'orange':34, 'blue':1, 'cargo yellow':35} And the following dataframe df_data = {'sentence': ['the summ

Solution 1:

Prepare input data:

data = {27695: 'I am legit happy because of these news.',
        143703: "Something seems suspicious.I can't recognize that...",
        ...
        48645: 'lol.',
        185265: 'Maybe there is a chance the infinity war sets are next'}

df = pd.DataFrame({"sentence": data.values()}, index=data.keys())

Find full words:

out = df.sentence.str.findall('|'.join([fr'\b{w}\b'for w in dictionary.keys()])) \
                     .apply(lambda l: ','.join(l))

Display only matched results:

>>> out[out != '']
56082      blue,orange
134801            blue
102078            blue
106617            blue
204968            blue
139796    cargo yellow
Name: sentence, dtype: object

Post a Comment for "Using Str.findall To Retrieve The Exact Match From Dictionary"