繁体   English   中英

用另一列中的值替换字符串的一部分

[英]replacing part of a string with value from another column

pandas DataFrame包含一个列,其中包含花括号中的描述和占位符:

descr                        replacement
This: {should be replaced}   with this

任务是用同一行中另一列的文本替换花括号中的文本。 遗憾的是,它不像以下那么容易:

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

将list comprehension与re.sub ,特别是如果性能很重要:

import re

df['new'] = [re.sub(r"{.*?}", b, a) for a, b in zip(df['descr'], df['replacement'])]
print (df)
                        descr replacement              new
0  This: {should be replaced}   with this  This: with this
1                This: {data}         aaa        This: aaa

您的代码使用的是Pandas.Series.str.replace() ,它需要两个字符串来执行替换操作,但第二个参数是Series。

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

用一些其他字符串替换Series / Index中出现的pattern / regex。 相当于str.replace()或re.sub()。 参数:

pat:字符串或编译的正则表达式

repl:string或callable ...

您可以直接使用Pandas.Series.replace()方法更正它:

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

观察:

我改变了你的正则表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM