Rocky5
Rocky5

Reputation: 47

Remove string if not followed by another string

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.

Upvotes: 2

Views: 253

Answers (3)

The fourth bird
The fourth bird

Reputation: 163632

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

Upvotes: 0

you can use regular expression :

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

Upvotes: 0

not_speshal
not_speshal

Reputation: 23166

Use rstrip:

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

Upvotes: 6

Related Questions