【发布时间】:2020-02-13 17:47:36
【问题描述】:
您好,我有一个数据框,我想从以它开头的每一行中删除一组特定的字符“fwd”。我面临的问题是我用来执行此操作的代码正在删除以字母“f”开头的任何内容。
我的数据框如下所示:
summary
0 Fwd: Please look at the attached documents and take action
1 NSN for the ones who care
2 News for all team members
3 Fwd: Please take action on the action needed items
4 Fix all the mistakes please
当我使用代码时:
df['Clean Summary'] = individual_receivers['summary'].map(lambda x: x.lstrip('Fwd:'))
我最终得到一个如下所示的数据框:
summary
0 Please look at the attached documents and take action
1 NSN for the ones who care
2 News for all team members
3 Please take action on the action needed items
4 ix all the mistakes please
我不希望最后一行在“修复”中丢失 F。
【问题讨论】:
-
这将删除以字母“f”开头的所有内容这是意料之中的,请查看
str.lstrip()的文档。 -
这能回答你的问题吗? Strip removing more characters than expected
-
是的,我明白了,有没有更好的方法来专门删除仅使用 'fwd:' 的案例
-
Regex 可能是你最好的选择。
-
另一个选项是:
individual_receivers['summary'].str.lstrip('Fwd:').where(individual_receivers['summary'].str.startswith('Fwd:'),individual_receivers['summary'])