2

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.

1
  • Just out of curiosity does someone knows why the "Have tried" section did not work? Commented Aug 10, 2021 at 7:39

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this works! Preferred the solution without additional module as the regex is well known for not being beginner user friendly.
2

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

3 Comments

I wasn't familiar with partition. Is this the right one? docs.python.org/3/library/stdtypes.html#str.partition
@merced yes that’s it
Thank you, this works, however be notified that the XXX in df['XXX'] is case sensitive so if it doesn't work right away check the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.