Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str.replace starting from the back in pandas DataFrame

I have two columns like so:

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

I want to remove the last occurrence of s in string. For context, my DataFrame has hundreds of thousands of rows. I know about str.replace and str.rfind, but nothing that does the desired combination of both, and I'm coming up blank in improvising a solution.

Thanks in advance for any help!

like image 268
user49007 Avatar asked Mar 09 '23 07:03

user49007


1 Answers

You can use rsplit and join:

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

Output:

0    the 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  the best new york cheesecake ny  new york      1
1           houston public school    houston      1
like image 189
Scott Boston Avatar answered Mar 11 '23 00:03

Scott Boston