asd
asd

Reputation: 1309

Match regex containing whole word using list

I have the code below that works

lista= ['abd','bda']
for element in lista:
    df[element] = df['concatenated_string'].str.contains(element, regex=True)

df
          concatenated_string
0         abdar___
1         abd___
2         asd_ab_ad______

How could I match on the whole word instead? So a value for "abdar" in the concatenated_string column should not be picked up. I tried:

for element in lista:
    df[element] = df['concatenated_string'].str.contains("\b(element)\b", regex=True)

Upvotes: 3

Views: 57

Answers (2)

Sergey Bushmanov
Sergey Bushmanov

Reputation: 25189

Try f-string, literal string interpolation:

df = pd.DataFrame({"a":["I was", "I wasn't"]})
els = ["was"]

for element in els:
    df["element"] = df["a"].str.contains(f"\\b{element}\\b", regex=True)
    
df
          a  element
0     I was     True
1  I wasn't    False

Upvotes: 2

gtomer
gtomer

Reputation: 6564

Try this:

for element in lista:
    df[element] = df['concatenated_string'].str.contains(" " +element+ " ", regex=True)

Upvotes: 0

Related Questions