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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With