Similar question to : Replacing part of string in python pandas dataframe
However it wont work!?
Panas 23.4
Given the following df column:
Expression
XYZ&(ABC|DEF)
(HIJ&FTL&JKK)&(ABC|DEF)
(FML|AXY|AND)&(ABC|DEF)
I want to strip a substring that may be in each column.
flag = '(ABC|DEF)'
andFlag = '&' + flag #the reasoning for doing this is that 'flag' may change
#Below are all different ways I have tried to achieve this, none have worked.
df['Expression'] = df['Expression'].replace(andFlag, '', regex=True)
df['Expression'] = df['Expression'].apply(lambda x: re.sub(andFlag, '', x))
df['Expression'] = df['Expression'].replace(to_replace=andFlag, value= '', regex=True)
df['Expression'] = df['Expression'].str.replace(andFlag, '')
df['Expression'] = df['Expression'].str.replace(andFlag, '', regex=True)
I have tried all of these functions with and without regex=True
to no avail.
Expected output:
Expression
XYZ
(HIJ&FTL&JKK)
(FML|AXY|AND)
I'm going slightly crazy trying to figure this out, it seems so simple and straightforward.