Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';和';string.contains中的运算符_Python_String_Pandas_Series - Fatal编程技术网

Python ';和';string.contains中的运算符

Python ';和';string.contains中的运算符,python,string,pandas,series,Python,String,Pandas,Series,我有一个熊猫系列,在其中我以这种方式应用字符串搜索 df['column_name'].str.contains('test1') 这将根据字符串“test1”是否包含在列“column_name”中给出真/假列表 但是,我无法测试两个字符串,我需要检查这两个字符串是否都存在。差不多 df['column_name'].str.contains('test1' and 'test2') 这似乎不起作用。任何建议都很好 all( word in df['column_name'] for

我有一个熊猫系列,在其中我以这种方式应用字符串搜索

df['column_name'].str.contains('test1')
这将根据字符串“test1”是否包含在列“column_name”中给出真/假列表

但是,我无法测试两个字符串,我需要检查这两个字符串是否都存在。差不多

  df['column_name'].str.contains('test1' and 'test2')
这似乎不起作用。任何建议都很好

all( word in df['column_name'] for word in ['test1', 'test2'] )
这将测试字符串中的任意数字或单词


这将测试字符串中的任意数字或单词

否您必须创建2个条件并使用
&
,并根据运算符优先级在条件周围加括号:

(df['column_name'].str.contains('test1')) & (df['column_name'].str.contains('test2))
如果您想测试其中一个单词,那么以下方法可以工作:

df['column_name'].str.contains('test1|test2')

否您必须创建2个条件并使用
&
,并根据运算符优先级在条件周围加括号:

(df['column_name'].str.contains('test1')) & (df['column_name'].str.contains('test2))
如果您想测试其中一个单词,那么以下方法可以工作:

df['column_name'].str.contains('test1|test2')

忽略
'test2
中缺少的引号,'and'运算符是一个布尔逻辑运算符。它不会连接字符串,也不会执行您认为它会执行的操作

>>> 'test1' and 'test2'
'test2'
>>> 'test1' or 'test2'
'test1'
>>> 10 and 20
20
>>> 10 and 0
10
>>> 0 or 20
20
>>> # => and so on...
这是因为
运算符起着“真值判定者”的作用,并且对字符串的行为有些奇怪。本质上,返回值是最后一个被求值的值,不管它是字符串还是其他。看看这种行为:

>>> a = 'test1'
>>> b = 'test2'
>>> c = a and b
>>> c is a
False
>>> c is b
True
后一个值被分配给我们要给它赋值的变量。您要寻找的是一种迭代列表或字符串集的方法,并确保所有这些结果都为true。为此,我们使用
all(iterable)
函数

if all([df['column_name'].contains(_) for _ in ['test1', 'test2']]):
    print("All strings are contained in it.")
else:
    print("Not all strings are contained in it.")
假设情况属实,以下是您将收到的示例:

>>> x = [_ in df['column_name'] for _ in ['test1', 'test2']
>>> print(x)
[True, True] # => returns True for all()
>>> all(x)
True
>>> x[0] = 'ThisIsNotIntTheColumn' in df['column_name']
>>> print(x)
[False, True]
>>> all(x)
False

忽略
'test2
中缺少的引号,'and'运算符是一个布尔逻辑运算符。它不会连接字符串,也不会执行您认为它会执行的操作

>>> 'test1' and 'test2'
'test2'
>>> 'test1' or 'test2'
'test1'
>>> 10 and 20
20
>>> 10 and 0
10
>>> 0 or 20
20
>>> # => and so on...
这是因为
运算符起着“真值判定者”的作用,并且对字符串的行为有些奇怪。本质上,返回值是最后一个被求值的值,不管它是字符串还是其他。看看这种行为:

>>> a = 'test1'
>>> b = 'test2'
>>> c = a and b
>>> c is a
False
>>> c is b
True
后一个值被分配给我们要给它赋值的变量。您要寻找的是一种迭代列表或字符串集的方法,并确保所有这些结果都为true。为此,我们使用
all(iterable)
函数

if all([df['column_name'].contains(_) for _ in ['test1', 'test2']]):
    print("All strings are contained in it.")
else:
    print("Not all strings are contained in it.")
假设情况属实,以下是您将收到的示例:

>>> x = [_ in df['column_name'] for _ in ['test1', 'test2']
>>> print(x)
[True, True] # => returns True for all()
>>> all(x)
True
>>> x[0] = 'ThisIsNotIntTheColumn' in df['column_name']
>>> print(x)
[False, True]
>>> all(x)
False

您想知道
test1
test2
是否在列中的某个位置


因此
df['col\u name'].str.contains('test1').any()&df['col\u name'].str.contains('test2').any()
您想知道
test1
test2
是否在列中的某个位置


因此
df['col\u name'].str.contains('test1').any()&df['col\u name'].str.contains('test2').any()

您可以通过移除最外层的方括号,将列表理解更改为更高效的生成器。然后将不会创建新列表,并且
all
将能够短路。“all”给我一个真/假值。无论test1和test2是否存在,我都需要对colubomn中的每个条目进行查询。所以,如果列的名称是400长,我想知道其中有多少是“test1”,有多少是“test2”。到目前为止,EdChums的回答最有意义。您的问题指出您需要同时检查这两个方面,我假设您想同时检查,因为您使用了
运算符,而不是
,您可以通过移除最外层的方括号将列表理解更改为更高效的生成器。然后将不会创建新列表,并且
all
将能够短路。“all”给我一个真/假值。无论test1和test2是否存在,我都需要对colubomn中的每个条目进行查询。所以,如果列的名称是400长,我想知道其中有多少是“test1”,有多少是“test2”。到目前为止,EdChums的回答最有意义。您的问题说明您需要检查这两个选项,我假设您希望同时执行,因为您使用了
运算符,而不是
运算符。谢谢在“test2”中添加了缺少的引号。谢谢您提供了详细的答案。我在这方面学到了很多东西,但不确定是否可以直接使用其中任何一个来实现我想要实现的目标。请看第三段代码。这应该会达到你想要的效果。谢谢大家。如何获得列名称条目中匹配数和未命中数的计数。我在EdChums解决方案上使用vale_counts()命令,它正在工作。谢谢在“test2”中添加缺少的引号。谢谢详细的回答。我在这方面学到了很多东西,但不确定是否可以直接使用其中任何一个来实现我想要实现的目标。请看第三段代码。这应该会达到你想要的效果。谢谢大家。如何获得列名称条目中匹配数和未命中数的计数。我在EdChums解决方案上使用vale_counts()命令,该命令正在运行。感谢您向我介绍any(),但是在本要求中,我需要知道column_name中有多少项匹配,在多少项中,不感谢您向我介绍any(),但是在本要求中,我需要知道在列_name中有多少项匹配,有多少项不匹配