如何在Python中检查关键字是否存在于标题中?

huangapple go评论55阅读模式
英文:

How to check if keyword is present in title or not in Python?

问题

我有一个包含多个关键词的数据集我想检查标题是否包含相同的关键词匹配应该是逐行部分匹配

我尝试了这段代码但对印度语单词/关键词不起作用

```python
for string in df['key']:
    test = df['title'].str.contains(string, case=False, na=False) 
    print(test)

样本数据集:

如何在Python中检查关键字是否存在于标题中?

期望输出:

如何在Python中检查关键字是否存在于标题中?


<details>
<summary>英文:</summary>

I have a dataset which contains multiple keywords and I want to check if title contains the same key or not. it should be row by row partial match.

I have tried this code but it is not working for indic words/keys:

```python
for string in df[&#39;key&#39;]:
    test = df[&#39;title&#39;].str.contains(string,case=False, na=False) 
    print(test)

Sample dataset:

如何在Python中检查关键字是否存在于标题中?

Expected Output:

如何在Python中检查关键字是否存在于标题中?

答案1

得分: 0

我没有Indic键盘,但你尝试过简单的解决方案吗:

for line in list_of_lines:
    if str(line['key']).lower() in str(line['title']).lower():
        print('True')
    else:
        print('False')
英文:

I don't have Indic keyboard, but have you tried the easy solution:

for line in list_of_lines:
    if str(line[&#39;key&#39;]).lower() in str(line[&#39;title&#39;]).lower():
        print(&#39;True&#39;)
    else:
        print(&#39;False&#39;)

huangapple
  • 本文由 发表于 2023年4月17日 21:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定