简体   繁体   English

当字符串中有 substring 时,检查并返回 Boolean

[英]Check and return Boolean when there is substring in string

Hi I am searching only the exact substring from string column and return True/False.嗨,我只从字符串列中搜索确切的 substring 并返回 True/False。

在此处输入图像描述

Row-3,4,5 has sting 'abc' (case-sensitive) but when i tried to return it returns TRUE for all rows第 3、4、5 行有 sting 'abc'(区分大小写),但是当我尝试返回时,它为所有行返回 TRUE
Below is code i have tried.下面是我尝试过的代码。

df['try_output'] = df['String1'].str.contains('ABC',case = False)

Is there any modification in above statement to get output column ' Required_Output '上述语句中是否有任何修改以获得 output 列“ Required_Output

I don't think str.contains is what you are looking for here, rather, you are looking for an exact match that will not consider upper / lower cases.我不认为str.contains是您在这里寻找的东西,而是您正在寻找不考虑大写/小写的完全匹配。 Therefore, you can simply convert to upper, str.upper() , and check whether it equals to 'ABC':因此,您可以简单地转换为上层str.upper() ,并检查它是否等于 'ABC':

df['output'] = df.string_1.str.upper() == 'ABC'

print(df)

  string_1  output
0      ABC    True
1      abc    True
2   XYZabc   False
3   XyzABC   False
4  ABCqqqq   False
5      AbC    True
6      aBC    True

It's logical why your code returns everything TRUE - all of your rows contain 'abc', especially when you specify not to care about upper cases ( case = False )您的代码返回所有内容TRUE是合乎逻辑的 - 您的所有行都包含“abc”,特别是当您指定不关心大写时( case = False

Use str.fullmatch ( Pandas >= 1.1.0 ) without any conversion:使用str.fullmatch ( Pandas >= 1.1.0 ) 无需任何转换:

df['output'] = df['string_1'].str.fullmatch('abc', case=False)
print(df)

# Output:
  string_1  output
0      ABC    True
1      abc    True
2   XYZabc   False
3   XyzABC   False
4  ABCqqqq   False
5      AbC    True
6      aBC    True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM