我有一个数据框,我在其中进行了一些字符串替换,例如:
df1["Restriction"] = df1["Restriction"].str.replace("vs", "&")
结果是这样的:
Restriction
Montreal & Vancouver
Montreal &
我想要做的是删除 & 如果它后面没有另一个字符串,那么它会导致以下结果:
Restriction
Montreal & Vancouver
Montreal
我想过切片最后一个字符,但那将切片所有。任何建议表示赞赏。
谢谢!
使用rstrip
:
df1["Restriction"] = df1["Restriction"].str.replace("vs", "&").str.rstrip("& ")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句