Home > database >  Strip colum values if startswith a specific string pandas
Strip colum values if startswith a specific string pandas

Time:01-19

I have a pandas dataframe(sample).

id  name
1   Mr-Mrs-Jon Snow
2   Mr-Mrs-Jane Smith
3   Mr-Mrs-Darth Vader

I'm looking to strip the "Mr-Mrs-" from the dataframe. i.e the output should be:

id  name
1   Jon Snow
2   Jane Smith
3   Darth Vader

I tried using

df['name'] = df['name'].str.lstrip("Mr-Mrs-")

But while doing so, some of the alphabets of names in some rows are also getting stripped out.

I don't want to run a loop and do .loc for every row, is there a better/optimized way to achieve this ?

CodePudding user response:

Don't strip, replace using a start of string anchor (^):

df['name'] = df['name'].str.replace(r"^Mr-Mrs-", "", regex=True)

Or removeprefix:

df['name'] = df['name'].str.removeprefix("Mr-Mrs-")

Output:

id         name
1      Jon Snow
2    Jane Smith
3   Darth Vader
  • Related