1

I have 2 pandas columns, one has filepath and another column has new folder name, i am trying to replace the folder name with new folder name using regex replace

df['new_path'] = df.root.str.replace(r'A-[0-9]*-END', df.new_folder_name)

I am getting an error repl must be a string or callable , is it possible to replace matched regex with value from corresponding column?

1
  • I don't think str.replace allow it. You may extract values by the regex pattern and combine them with df.new_folder_name to construct a dict to use with replace Commented Sep 20, 2019 at 2:34

1 Answer 1

1

You can compile the pattern first and then use apply:

import pandas as pd
import re

df = pd.DataFrame({"filename":[1,2,3],
                   "filepath":["C:/A-1-END","C:/A-12342-END","D:/A-777-END"],
                   "new_folder_name":["newfolder1","newfolder2","newfolder3"]})

pat = re.compile(r"A-[0-9]*-END", re.IGNORECASE)

df["new_path"] = df[["filepath","new_folder_name"]].apply(lambda x: pat.sub(repl=x[1],string=x[0]),axis=1)

Result:

   filename        filepath new_folder_name       new_path
0         1      C:/A-1-END      newfolder1  C:/newfolder1
1         2  C:/A-12342-END      newfolder2  C:/newfolder2
2         3    D:/A-777-END      newfolder3  D:/newfolder3
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Henry, any idea why is this stackoverflow.com/questions/67810759/… not working?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.