Remove string if not followed by another string

Viewed 114

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.

3 Answers

Use rstrip:

df1["Restriction"] = df1["Restriction"].str.replace("vs", "&").str.rstrip("& ")

you can use regular expression :

df1["Restriction"] = df1["Restriction"].str.replace("&$","")

If you don't want vs to be part of a partial word, you can use a regex with an alternation and a capture group, and surround vs with word boundaries \b

In the capture group, match vs till the end of the string, or else match vs as an alternative. If capture group 1 exists, replace with an empty string, else replace with &

import pandas as pd

texts = [
    "Montreal vs Vancouver",
    "Montreal vs",
    "devs"
]

df1 = pd.DataFrame(texts, columns=["Restriction"])

df1['Restriction'] = df1['Restriction'].str.replace(
    r'(\s*\bvs\s*$)|\bvs\b',
    lambda m: '' if m.group(1) else '&'
)

print(df1)

Output

            Restriction
0  Montreal & Vancouver
1              Montreal
2                  devs
Related