0

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

[apple, banana]

In this dataset, matching column 'Value' - [keyword list]

*I mean matching is keyword in list in 'Value'

I would like to see how the keywords in the list match column, so.. I want to find out how much the matching rate is.

Ultimately, what I want to know is 'Finding match rate between keywords and columns' Percentage, If I can, filtered dataframe

Thank you.

Edit

In my real dataset, There are keywords in the sentence,

Ex,

Ilikeapplethanbananaandorange

so It doesn`t work if use keyword - keyword matching(1:1).

5
  • yes, percentage, and If I can, get matching dataframe. Commented Feb 3, 2020 at 1:13
  • Could you show me how apply that ex code? Commented Feb 3, 2020 at 1:14
  • df.shape[1] value is '2', do you mean df.shape[0]? It`s value '5' Commented Feb 3, 2020 at 1:22
  • In my real dataset, there are keywords in the sentence.. that code doesn`t work.. Commented Feb 3, 2020 at 1:33
  • Yes sry, I edited Commented Feb 3, 2020 at 1:34

1 Answer 1

2

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"

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explicate "|" is? Is it just mean space?
See more explanation in my answer after dit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.