首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:文本匹配

Python:文本匹配
EN

Stack Overflow用户
提问于 2021-06-04 01:55:23
回答 4查看 717关注 0票数 0

我有一个像这样的数据库:

代码语言:javascript
复制
df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']

我想要做的是迭代,并找到这些值,但它不起作用:

代码语言:javascript
复制
for i in range(len(df['col']):
   if df.loc[i, 'col'] in list(['text', 'pro']):
   print('yes')

我想要的输出:

代码语言:javascript
复制
yes
yes
yes
yes
yes
yes
yes
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-06-04 02:09:57

下面的答案应该是你所要求的。请注意,为了更好地使用您的DataFrame,我重新构造了一些项。(根据你的代码,我想你是和熊猫一起工作的)。

代码语言:javascript
复制
import pandas

# outsource the matching to a function so we can use `map` below
def match_string(input_string):
    """
    """
    target_strings = ['text', 'pro']
    for target in target_strings:
        # you need to lower your strings if you want the match to be case-insensitive
        if target in input_string.lower():
            print("yes")
            return "yes"
    print("no")
    return "no"

df = pandas.DataFrame()

df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']
# instead of iterating, use a map operation which is much more efficient
# this will store the result in the column 'string_matched' as well as print it out as the question requests
df["string_matched"] = df["col"].map(lambda x: match_string(x))

你可以找到一些关于熊猫地图这里的文档。

输出

代码语言:javascript
复制
yes
yes
yes
yes
yes
yes
yes
no
票数 0
EN

Stack Overflow用户

发布于 2021-06-04 02:09:35

这就是你要找的吗?基本上,您需要检查列中的'text''pro'。因此,您应该将这两个字符串列进行比较。

代码语言:javascript
复制
import pandas as pd
df=pd.DataFrame()
df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']
list1=[]
for i in range(len(df['col'])):
    
    if  'text' in str(df['col'][i]).lower() or 'pro' in str(df['col'][i]).lower():
        list1.append("yes")
    else:
        list1.append("no")
df['Test/Pro']=list1
print(df)

输出:

代码语言:javascript
复制
            col Test/Pro
0          text      yes
1         Texts      yes
2      Text-Pro      yes
3  Text;Nothing      yes
4           Pro      yes
5           pro      yes
6          Pros      yes
7       Nothing       no
票数 0
EN

Stack Overflow用户

发布于 2021-06-04 02:09:56

您可以使用regex和布尔掩蔽。您可能需要更新regex以匹配您的用例。

代码语言:javascript
复制
import pandas as pd

df = pd.DataFrame()

df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']

masking = df['col'].str.contains(r'(Text|text)|(Pro|pro)')

print(masking)

版画

代码语言:javascript
复制
0     True
1     True
2     True
3     True
4     True
5     True
6     True
7    False
Name: col, dtype: bool
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67830549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档