如何用一个特定的单词(即ABC)替换一个字母数字/数字,如下所示在熊猫中
输入数据
what is ABC s123 doing 77 here?
what is abc aA574 doing 89 here?
what is ABC-X187 doing here?
what is aBC^984 doing here?
what is Abc647 doing here?
预期的输出数据
what is ABCS123 doing 77 here?
what is ABCAA574 doing 89 here?
what is ABCX187 doing here?
what is ABC984 doing here?
what is ABC647 doing here?
注意:任何字母数字都可以跟随ABC。此处显示的数字仅是示例,请勿在解决方案中对数字进行硬编码。
EDIT1:刚尝试了建议的解决方案。当特殊字符为空格时,它将不起作用。因此,请删除重复的标签。
EDIT2:请按照问题处理ABC。
答案 0 :(得分:3)
您可以使用:
df['col'] = df['col'].str.replace(r'(?<=ABC)\W+(?=\d\d\d)', '')
或
df['col'] = df['col'].map(lambda x: re.sub(r'(?<=ABC)\W+(?=\d\d\d)', '', x))
答案 1 :(得分:3)
您可以使用以下代码:
import re
regex = r"(.*[A-Z]+).*?(\d+.*)"
test_str = """what is ABC 123 doing here?
what is ABC 574 doing here?
what is ABC-187 doing here?
what is ABC^984 doing here?
what is ABC647 doing here?"""
subst = r"\1\2"
result = re.sub(regex, subst, test_str)
print (result)
# what is ABC123 doing here?
# what is ABC574 doing here?
# what is ABC187 doing here?
# what is ABC984 doing here?
# what is ABC647 doing here?
regex101上的详细信息:https://regex101.com/r/gGK8fJ/2
答案 2 :(得分:2)
摘自Series.str.replace的文档
s = pd.Series("""what is ABC 123 doing here?
what is ABC 574 doing here?
what is ABC-187 doing here?
what is ABC^984 doing here?
what is ABC647 doing here?""".split("\n"))
pattern = r"ABC.*?(\d+)"
s.str.replace(pattern, r"ABC \1")
0 what is ABC 123 doing here? 1 what is ABC 574 doing here? 2 what is ABC 187 doing here? 3 what is ABC 984 doing here? 4 what is ABC 647 doing here? dtype: object