我有两列是这样的:
string s
0 the best new york cheesecake new york ny new york
1 houston public school houston houston
我想删除最后一次出现s
在string
。对于上下文,我的DataFrame具有成千上万的行。我知道str.replace
和str.rfind
,但是没有任何事情可以同时实现两者的理想组合,因此我在改进解决方案方面显得空白。
在此先感谢您的帮助!
您可以使用rsplit
和join
:
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] 删除。
我来说两句