I am trying to separate few string from pandas dataframe :
x = pd.DataFrame()
x['y'] = ["Hernia|Infiltration","A|Hernia|Infiltration","Infiltration|Hernia"]
x
I am executing below code :
x['y'] = x['y'].replace({'|Hernia': ''},regex=True)
x['y'] = x['y'].str.replace('Hernia|', '',regex=True)
x
But output is wrong :
wrong output :
y
0 |Infiltration
1 A||Infiltration
2 Infiltration|
Correct/ Expected output
y
0 Infiltration
1 A|Infiltration
2 Infiltration
There can be any string in place of A and Infiltration , but pattern would be same.
regex=Truewhen you're trying to replace a literal string rather than a regular expression?