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?
You may want to rather replace a single character followed by
n-1
characters at the end of the 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.