How to do a backreference in Pandas <columnSeries>.str.replace
?
I want to access only the pattern in the parenthesis for each of the values in a dataframe column. So I have done this.
df['column'] = df['column'].str.replace(r'(<somePattern>)<someOtherPattern>', '\1')
But it doesn't seem to work.
I have read the documentation of Pandas and re.sub
is being used under the hood for all the replaces. The Python documentation reads \1
, \2
etc. for the backreferences.
What am I doing wrong here !
Use r'\1'
or '\\1'
instead of '\1'
, as suggested in the comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With