I have a data frame where I do some string replacing for example:
df1["Restriction"] = df1["Restriction"].str.replace("vs", "&")
which results in something like this:
Restriction
Montreal & Vancouver
Montreal &
What I would like to do is remove the & if it is not followed by another string so it would result in the following:
Restriction
Montreal & Vancouver
Montreal
I thought about slicing the last characters but then that would slice for everything. Any suggestions are appreciated.
Thank you!
CodePudding user response:
Use rstrip
:
df1["Restriction"] = df1["Restriction"].str.replace("vs", "&").str.rstrip("& ")
CodePudding user response:
you can use regular expression :
df1["Restriction"] = df1["Restriction"].str.replace("&$","")