Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backreference in Pandas str.replace

Tags:

python

pandas

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 !

like image 971
mythicalcoder Avatar asked Sep 16 '25 22:09

mythicalcoder


1 Answers

Use r'\1' or '\\1' instead of '\1', as suggested in the comments.

like image 110
EliadL Avatar answered Sep 19 '25 11:09

EliadL