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)
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
Try this:
for element in lista:
df[element] = df['concatenated_string'].str.contains(" " +element+ " ", regex=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.