0

This code used to work in older pandas versions (0.24.2), but throws error in current version (1.3.0): *ValueError: Series.replace cannot use dict-like to_replace and non-None value*.
import pandas as pd

a = pd.DataFrame([['the quick red fox jumps over the lazy dog','red|over'],['red is the color for danger','red']], columns=['text','match'])
a['text'].replace(r'('+a['match']+')', r'<*\1*>', regex=True, inplace=True)

print(a)

How can I make it work on the newer version?

1 Answer 1

1

IIUC:

try:

a['text']=a['text'].replace('('+'|'.join(a['match'].tolist())+')', r'<*\1*>', regex=True)

output of a:

    text                                                  match
0   the quick <*red*> fox jumps <*over*> the lazy dog   red|over
1   <*red*> is the color for danger                      red
Sign up to request clarification or add additional context in comments.

Comments

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.