python - 使用 Python / Pandas 和可能的正则表达式从全名列表中提取姓氏
问题描述
我正在处理一个数据集,我最终得到了一个如下形式的名称列表:
s = ['DR. James Coffins',
'Zacharias Pallefas',
'Matthew Ebnel',
'Ranzzith Redly',
'GEORGE GEORGIADAKIS',
'HARISH KUMARAN K',
'Christiaan Kraanlen, CFA',
'Mary K. Lein, CFA, COL',
'Alexandre Cegra, CFA, CAIA'
'Anna Bely']
我必须提取姓氏并将它们放在单独的列表中(或熊猫数据框中的列)。但是,我对全名的多态性感到困惑,而且我是 Python 的新手。
可能的算法如下:
Loop through the elements of the list. For each element:
split the element into subelements using spaces. Then:
a) If there are four or less subelements start from the beginning and
examine the first four subelements.
a1) If the first subelement is larger than 2 letters then: If the
second subelement is larger than one letter, return the second
subelement. Otherwise, return the third subelement.
a2) if the first subelement is 2 letters then drop it and repeat
step a1
解决方案
在跳过包含.
但不在排除列表中的单词后总是抓住每行的第二个元素怎么样['dr', 'mr', 'mrs', 'mrs', 'miss', 'prof']
>>> exclude_tags = ['dr', 'mr', 'mrs', 'mrs', 'miss', 'prof']
>>> [[y for y in x.split() if '.' not in y and y.lower() not in exclude_tags][1].rstrip(',').capitalize() for x in s]
['Coffins', 'Pallefas', 'Ebnel', 'Redly', 'Georgiadakis', 'Kumaran', 'Kraanlen', 'Lein', 'Cegra']
推荐阅读
- python - 使用 AWS 负载均衡器将来自 EKS 的 FastAPI 公开给外部世界
- kubernetes - 开始使用 KrakenD
- javascript - 在另一个类中的方法之前执行ajax调用和控制器
- asp.net - ASP.NET Cors > 只允许一个来源
- javascript - 处理使用地图函数javascript呈现的多个输入框
- python - Python - 是否有使用 GA4 admin api 创建自定义维度的特定方法?
- swift - 将 CallbackInstrument 添加到混音器时,AudioKit 混音器的最后一个通道正在翻边
- php - 如何在 for 循环中执行此操作?
- node.js - 当我在heroku上上传我的网站进行生产时,我遇到了这个问题,这个错误有什么解决方案吗?
- filebeat - 有没有办法使用 ELK 从 HTTP GET 请求中解析 HTML?