aerin
aerin

Reputation: 22694

python pandas.Series.str.contains words with space

I'm trying to find strings that contain either " internet ", " program ", " socket programming " in the pandas dataframe.

df.col_name.str.contains(" internet | program | socket programming ", case=False)

Is this right way to do so? or Do I need to escape space using \ and raw string?

Upvotes: 2

Views: 4564

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210912

Here is a small demo:

In [250]: df
Out[250]:
                                      txt
0                                Internet
1  There is no Internet in this apartment
2                                Program2
3    I am learning socket programming too

In [251]: df.txt.str.contains(" internet | program | socket programming ", case=False)
Out[251]:
0    False
1     True
2    False
3     True
Name: txt, dtype: bool

If you want to "match" also the first row: Internet:

In [252]: df.txt.str.contains(r"\b(?:internet|program|socket\s+programming)\b", case=False)
Out[252]:
0     True
1     True
2    False
3     True
Name: txt, dtype: bool

Upvotes: 5

Related Questions