Reputation: 1309
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
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
Reputation: 6564
Try this:
for element in lista:
df[element] = df['concatenated_string'].str.contains(" " +element+ " ", regex=True)
Upvotes: 0