Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我可以删除名称前缀而不污染名称数据吗?_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 我可以删除名称前缀而不污染名称数据吗?

Python 3.x 我可以删除名称前缀而不污染名称数据吗?,python-3.x,pandas,Python 3.x,Pandas,我已尝试从名称中删除前缀。现在我使用re.sup方法删除前缀,但有些名称包含前缀中包含的字符 数据示例 我尝试了re.sub(r'(^\w{2,5}\?)',r'',name)来删除带有fix-the-position的前缀,但它不起作用,因为我有10多个前缀,每个前缀的大小不同 import re name = 'mrjasontoddmr' filter_name = re.sub(r'mr', r'', name) print(filter_name) #The result of fi

我已尝试从名称中删除前缀。现在我使用re.sup方法删除前缀,但有些名称包含前缀中包含的字符

数据示例 我尝试了re.sub(r'(^\w{2,5}\?)',r'',name)来删除带有fix-the-position的前缀,但它不起作用,因为我有10多个前缀,每个前缀的大小不同

import re
name = 'mrjasontoddmr'
filter_name = re.sub(r'mr', r'', name)
print(filter_name)

#The result of filer_name is jasontodd but what I want is jasontoddmr

我希望“jasonoddmr”的输出可以指定计数,并使用re.sub()中提供的参数忽略大小写

表示字符是可选的,因此在
mrs?\?
s和
中,字符是可选的,因此它可以捕获
mr或mr.
mrs或mrs.

import re
name = 'mrjasontoddmr'
filter_name = re.sub(r'mr', r'', name)
print(filter_name)

#The result of filer_name is jasontodd but what I want is jasontoddmr
import re
names = ['MisterClarkKent','Mrs.Carol','missjanedoemiss', 'mrjasontoddmr']
filter_names = [re.sub(r'mrs?\.?|mister\s?|miss\s?', r'',name, count=1, flags=re.IGNORECASE) for name in names]

filter_names

Out[99]: ['ClarkKent', 'Carol', 'janedoemiss', 'jasontoddmr']