how to replace part of string using regular expression?


I have a dataframe like this:


    well    tag
0   10B23   15B22|TestSep_OutletFlow
1   10B23   15B22|TestSep_GasOutletFlow
2   10B23   15B22|TestSep_WellNum
3   10B23   15B22|TestSep_GasPresValve
4   10B23   15B22|TestSep_Temp
5   10B23   WHT

Now I’d like to replace anything before | in column of tag to a string like 11A22, so the dataframe after replace should look like this:

well    tag
0   10B23   11A22|TestSep_OutletFlow
1   10B23   11A22|TestSep_GasOutletFlow
2   10B23   11A22|TestSep_WellNum
3   10B23   11A22|TestSep_GasPresValve
4   10B23   11A22|TestSep_Temp
5   10B23   WHT

I am thinking to use regular expression with group to replace group by a string, something in my mind look like this


then i got result of

well    tag tag2
0   10B23   15B22|TestSep_OutletFlow    11A2211A22B11A2211A22|11A2211A2211A22O11A2211A...
1   10B23   15B22|TestSep_GasOutletFlow 11A2211A22B11A2211A22|11A2211A2211A22G11A2211A...
2   10B23   15B22|TestSep_WellNum   11A2211A22B11A2211A22|11A2211A2211A22W11A2211A...
3   10B23   15B22|TestSep_GasPresValve  11A2211A22B11A2211A22|11A2211A2211A22G11A2211A...
4   10B23   15B22|TestSep_Temp  11A2211A22B11A2211A22|11A2211A2211A22T11A2211A22
5   10B23   WHT 11A22W11A22H11A22T11A22

Thanks for your help

Asked By: roudan



(|) is a special character in regex, you need to escape it.

df["tag2"] = df["tag"].str.replace(r"^w*|", "11A22|", regex=True)

Output :


    well                          tag                         tag2
0  10B23     15B22|TestSep_OutletFlow     11A22|TestSep_OutletFlow
1  10B23  15B22|TestSep_GasOutletFlow  11A22|TestSep_GasOutletFlow
2  10B23        15B22|TestSep_WellNum        11A22|TestSep_WellNum
3  10B23   15B22|TestSep_GasPresValve   11A22|TestSep_GasPresValve
4  10B23           15B22|TestSep_Temp           11A22|TestSep_Temp
5  10B23                          WHT                          WHT
Answered By: Timeless
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.