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