Python pandas remove part from string after substring

Viewed 68

Trying to clear wrong text that comes after the "Model" value in the "Name" column.

df = pd.DataFrame([['ABC-12(s)', 'Some text ABC-12(s) wrong text'], ['ABC-45', 'Other text ABC-45 garbage text'], ['XYZ-LL', 'Another text XYZ-LL unneeded text']], columns = ['Model', 'Name'])
index Model Name
0 ABC-12(s) Some text ABC-12(s) wrong text
1 ABC-45 Other text ABC-45 garbage text
2 XYZ-LL Another text XYZ-LL unneeded text

Expected result:

index Model Name
0 ABC-12(s) Some text ABC-12(s)
1 ABC-45 Other text ABC-45
2 XYZ-LL Another text XYZ-LL

Have tried:

df["name"] = df["name"].str.partition(df["model"].to_string(), expand=False)

But that gives back the original string without changes or error. Like it could not find the delimiter within the "Name" cell. Seems like I'm missing something very simple.

2 Answers

Another solution, using re:

import re

df["Name"] = df.apply(
    lambda x: re.split(r"(?<=" + re.escape(x["Model"]) + r")\s*", x["Name"])[0],
    axis=1,
)
print(df)

Prints:

       Model                 Name
0  ABC-12(s)  Some text ABC-12(s)
1     ABC-45    Other text ABC-45
2     XYZ-LL  Another text XYZ-LL

You can partition in a list comprehension and then join the first two parts back.

df['name_mod'] = [''.join(name.partition(model)[:-1]) 
                  for name,model in zip(df['Name'], df['Model'])]

       Model                               Name             name_mod
0  ABC-12(s)     Some text ABC-12(s) wrong text  Some text ABC-12(s)
1     ABC-45     Other text ABC-45 garbage text    Other text ABC-45
2     XYZ-LL  Another text XYZ-LL unneeded text  Another text XYZ-LL
Related