I have a dataframe df:
tags
"a,b,c,d"
"c,q,k,t"
and a list of strings I need to search for:
searchList = ["a", "b"]
I need to add a new column to my dataframe named "topic". If a string from searchList appears in column "tags" I need to set the value in that row to bool True, otherwise bool False.
Endresult:
tags | topic
"a,b,c,d" | True
"c,q,k,t" | False
My code so far:
searchList = ["a", "b"]
pattern = '|'.join(searchfor)
df["topic"] = df.loc[(df["tags"].str.contains('|'.join(pattern), na=False)), True] = True
But I get the error:
KeyError: 'cannot use a single bool to index into setitem'
?