Grendel
Grendel

Reputation: 783

str.contains specific pattern in pandas

COL1

a-b(+)je
a-b(-)neee
a-gd(+)ee 
bb-e(+)bdbd
ad-b(-)ddh

and I would like to only get TRUE for row that do contain '(-)' pattern

I tried:

df['COL1'].str.contains('(-)')

but all row are responding as true because of the -in the string

Expected behavior :

FALSE
TRUE
FALSE
FALSE
TRUE
FALSE

I use that in a code :

np.where(df['COL1'].str.contains('(-)'), do something)

Upvotes: 1

Views: 63

Answers (1)

jezrael
jezrael

Reputation: 862611

Because () are special regex characters you can pass regex=False:

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

Or escape them by \:

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

Upvotes: 1

Related Questions