Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing part of a string with value from another column

A pandas DataFrame contains a column with descriptions and placeholders in curly braces:

descr                        replacement
This: {should be replaced}   with this

The task is to replace the text in the curly braces with text from another column in the same row. It's unfortunately not as easy as:

df["descr"] = df["descr"].str.replace(r"{*?}", df["replacement"])

~/anaconda3/lib/python3.6/site-packages/pandas/core/strings.py in replace(self, pat, repl, n, case, flags, regex)
   2532     def replace(self, pat, repl, n=-1, case=None, flags=0, regex=True):
   2533         result = str_replace(self._parent, pat, repl, n=n, case=case,
-> 2534                              flags=flags, regex=regex)
   2535         return self._wrap_result(result)
   2536 

~/anaconda3/lib/python3.6/site-packages/pandas/core/strings.py in str_replace(arr, pat, repl, n, case, flags, regex)
    548     # Check whether repl is valid (GH 13438, GH 15055)
    549     if not (is_string_like(repl) or callable(repl)):
--> 550         raise TypeError("repl must be a string or callable")
    551 
    552     is_compiled_re = is_re(pat)

TypeError: repl must be a string or callable
like image 746
clstaudt Avatar asked Jan 27 '23 14:01

clstaudt


1 Answers

Your code is using the Pandas.Series.str.replace() and it expects two strings to perform the replacement operation, but the second parameter is a Series.

Series.str.replace(pat, repl, n=-1, case=None, flags=0, regex=True)[source]

Replace occurrences of pattern/regex in the Series/Index with some other string. Equivalent to str.replace() or re.sub(). Parameters:

pat : string or compiled regex

repl : string or callable ...

You can correct it using directly the Pandas.Series.replace() method:

df = pd.DataFrame({'descr': ['This: {should be replaced}'],
                   'replacement': 'with this'
                  })
>> df["descr"].replace(r"{.+?}", df["replacement"], regex = True)
0    This: with this

Observation:

I changed a bit of your regexp.

like image 185
Daniel Labbe Avatar answered Mar 15 '23 08:03

Daniel Labbe