简体   繁体   English

如果以特定字符串开头,则去除列值 pandas

[英]Strip colum values if startswith a specific string pandas

I have a pandas dataframe(sample).我有一个 pandas 数据框(示例)。

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:我想从 dataframe 中去掉“Mr-Mrs-”。即 output 应该是:

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?我不想为每一行运行一个循环和 do.loc,是否有更好/优化的方法来实现这一点?

Don't strip, replace using a start of string anchor ( ^ ):不要剥离,使用字符串锚点 ( ^ ) 的开头replace

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

Or removeprefix :removeprefix

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

Output: Output:

id         name
1      Jon Snow
2    Jane Smith
3   Darth Vader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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