简体   繁体   中英

Strip colum values if startswith a specific string pandas

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. ie 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM