python关键字匹配(关键字列表-列) [英] python Keyword matching(keyword list - column)

查看:1905
本文介绍了python关键字匹配(关键字列表-列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假定的数据集,

    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]

在此数据集中,匹配列'Value'-[关键字列表]

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

*我的意思是匹配是'Value中列表中的关键字'

*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

谢谢。

编辑

在我的真实数据集中,句子中有关键字

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

Ex,

Ilikeapplethanbananaandorange

因此,如果使用关键字-关键字匹配(1:1),则无效。

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

推荐答案

使用 str.contains 将单词与句子匹配:

Use str.contains to match words to your sentences:

keywords = ['apple', 'banana']
df['Value'].str.contains("|".join(keywords)).sum() / len(df)

# 0.6

或者如果要保留行:


df[df['Value'].str.contains("|".join(keywords))]

  Name                Value
0    K          I eat apple
1    Y  banana is delicious
4    C          appleislike






更多详细信息

管道 | 是正则表达式中的运算符:

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:


匹配其中句子包含 apple或 banana 的行

这篇关于python关键字匹配(关键字列表-列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆