I am relatively new to regex and I am trying to replace part of the string inside of the string column in Pandas DataFrame. The challenge is that I have multiple string types that I want to remove from my column while keeping the rest of the string.
I have code working for 1 type of string, but when I try to use for loop, the code is not working. I am not sure how to specify iterator inside of the regex expression.
Here is code that works when applied to 1 type of sub-string:
df = pd.DataFrame({'A': ['ba ca t', 'foo', 'bait'],'B': ['abc', 'bar', 'xyz']})
df
df=df.replace({'A': r'^ba ca'}, {'A': ''}, regex=True)
df
Here is code that is not working when I try to us For Loop:
df = pd.DataFrame({'A': ['ba ca t', 'foo', 'bait'],'B': ['abc', 'bar', 'xyz']})
replace_list=['ba ca','foo']
for i in replace_list:
df=df.replace({'A': r'^(i)'}, {'A': ''}, regex=True)
df
I would like to iterate over a list of strings to remove them from a column in the DataFrame.