lmocsi
lmocsi

Reputation: 1096

Pandas dataframe regex replace not working in new version


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?

Upvotes: 0

Views: 320

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24324

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

Upvotes: 1

Related Questions