using str.replace() to remove nth character from a string in a pandas dataframe

1.4k views Asked by At

I have a pandas dataframe that consists of strings. I would like to remove the n-th character from the end of the strings. I have the following code:

DF = pandas.DataFrame({'col': ['stri0ng']})
DF['col'] = DF['col'].str.replace('(.)..$','')

Instead of removing the third to the last character (0 in this case), it removes 0ng. The result should be string but it outputs stri. Where am I wrong?

2

There are 2 answers

0
ernest_k On

You may want to rather replace a single character followed by n-1 characters at the end of the string:

DF['col'] = DF['col'].str.replace('.(?=.{2}$)', '')

      col
0  string

If you want to make sure you're only removing digits (so that 'string' in one special row doesn't get changed to 'strng'), then use something like '[0-9](?=.{2}$)' as pattern.

0
Chris On

Another way using pd.Series.str.slice_replace:

df['col'].str.slice_replace(4,5,'')

Output:

0    string
Name: col, dtype: object