python - str.replace starting from the back in pandas DataFrame -


i have 2 columns so:

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

i want remove last occurrence of s in string. context, dataframe has hundreds of thousands of rows. know str.replace , str.rfind, nothing desired combination of both, , i'm coming blank in improvising solution.

thanks in advance help!

you can use rsplit , join:

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

output:

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

edit:

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

output:

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

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -