从熊猫DataFrame的背面开始str.replace

用户名

我有两列是这样的:

                                       string                    s
0    the best new york cheesecake new york ny             new york
1               houston public school houston              houston

我想删除最后一次出现sstring对于上下文,我的DataFrame具有成千上万的行。我知道str.replacestr.rfind,但是没有任何事情可以同时实现两者的理想组合,因此我在改进解决方案方面显得空白。

在此先感谢您的帮助!

斯科特·波士顿

您可以使用rsplitjoin

df.apply(lambda x: ''.join(x['string'].rsplit(x['s'],1)),axis=1)

输出:

0    the best new york cheesecake  ny
1              houston public school 
dtype: object

编辑:

df['string'] = df.apply(lambda x: ''.join(x['string'].rsplit(x['s'],1)),axis=1).str.replace('\s\s',' ')

print(df)

输出:

                            string         s  third
0  the best new york cheesecake ny  new york      1
1           houston public school    houston      1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章