Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 str.在大熊猫中包含特定的模式_Python_String_Pandas - Fatal编程技术网

Python str.在大熊猫中包含特定的模式

Python str.在大熊猫中包含特定的模式,python,string,pandas,Python,String,Pandas,我只想为包含“(”模式的行设置为真 我试过: COL1 a-b(+)je a-b(-)neee a-gd(+)ee bb-e(+)bdbd ad-b(-)ddh 但由于字符串中的-,所有行都响应为true 预期行为: df['COL1'].str.contains('(-)') 我在代码中使用它: FALSE TRUE FALSE FALSE TRUE FALSE 因为()是特殊的正则表达式字符,所以可以传递regex=False: np.where(df['COL1'].str.co

我只想为包含“(”模式的行设置为真

我试过:

COL1

a-b(+)je
a-b(-)neee
a-gd(+)ee 
bb-e(+)bdbd
ad-b(-)ddh
但由于字符串中的
-
,所有行都响应为true

预期行为:

df['COL1'].str.contains('(-)')
我在代码中使用它:

FALSE
TRUE
FALSE
FALSE
TRUE
FALSE
因为
()
是特殊的正则表达式字符,所以可以传递
regex=False

np.where(df['COL1'].str.contains('(-)'), do something)
或者通过
\
将其转义:

print (df['COL1'].str.contains('(-)', regex=False))
0    False
1     True
2    False
3    False
4     True
Name: COL1, dtype: bool
print (df['COL1'].str.contains('\(-\)'))
0    False
1     True
2    False
3    False
4     True
Name: COL1, dtype: bool