Python Keyword Matching(keyword List - Column)
supposed dataset, Name Value 0 K Ieatapple 1 Y bananaisdelicious 2 B orangelikesomething 3 Q bluegrape 4 C appleislike and I have keyword list like [ap
Solution 1:
Use str.contains
to match words to your sentences:
keywords = ['apple', 'banana']
df['Value'].str.contains("|".join(keywords)).sum() / len(df)
# 0.6
Or if you want to keep the rows:
df[df['Value'].str.contains("|".join(keywords))]
Name Value
0 K I eat apple
1 Y banana is delicious
4 C appleislike
More details
The pipe |
is the or
operator in regular expression:
So we join our list of words with a pipe to match one of these words:
>>>keywords = ['apple', 'banana']>>>"|".join(keywords)
'apple|banana'
So in regular expression we have the statement now:
match rows where the sentence contains "apple" OR "banana"
Post a Comment for "Python Keyword Matching(keyword List - Column)"