Python 将pandas列中的字符串列表与正则表达式匹配
问题是:从字符串列表中查找所有专门包含子字符串的名称并返回该字符串Python 将pandas列中的字符串列表与正则表达式匹配,python,regex,pandas,Python,Regex,Pandas,问题是:从字符串列表中查找所有专门包含子字符串的名称并返回该字符串 def match(frame): result = [] for item in frame.name: if re.search('|'.join(boys), item) is not None: results = re.search('|'.join(boys), item) result.append(results) retu
def match(frame):
result = []
for item in frame.name:
if re.search('|'.join(boys), item) is not None:
results = re.search('|'.join(boys), item)
result.append(results)
return result
我有一个熊猫系列,例如:
231 richard occult (new earth)
6886 bedivere (new earth)
705 arthur pendragon (new earth)
567 franklin delano roosevelt (new earth)
1468 lancelot (new earth)
...
6891 nadine west (new earth)
6892 warren harding (new earth)
6893 william harrison (new earth)
6894 william mckinley (new earth)
6895 mookie (new earth)
6896 Superboy (new earth)
我有一个特定子字符串列表,我希望与每个名称匹配,即:
boy_names = ['Mr.', 'Boy', 'Man', 'Lord', 'King',
'Brother', 'Sir', 'Prince', 'Father', 'Lad',
'Baron', 'He-',' He' 'Son', 'Duke','Son','Dad', 'Senior',
'Junior', 'Master']
所需输出:
Superboy
我找到了一个返回匹配项的答案,但没有返回整个字符串
def match(frame):
result = []
for item in frame.name:
if re.search('|'.join(boys), item) is not None:
results = re.search('|'.join(boys), item)
result.append(results)
return result
其中boys是名字列表。我在这里运行代码,起初它没有返回任何内容。但后来我把re.IGNORECASE标志放在re.search cmd上,它返回了一个带有[son,boy]的列表(儿子来自william harrison,男孩来自Superboy)。你能再解释一下你想在这里完成什么吗?