python - 根据 pandas 中的特定子字符串或模式拆分字符串
问题描述
感谢您对我上一个问题的所有帮助。它很有用。
现在还有另一个问题。它基于我之前的问题。我有我的清理输入,我想拆分主要公司名称并根据一些子字符串或模式将其放在单独的列中。
这是我的输入:
Original_Input Cleansed_Input
Iris Diagnostics, a Division of Iris International Inc Iris Diagnostics a Division of Iris
International Inc
GINGI-PAK a division of The Belport Co., Inc. GINGIPAK a division of The Belport Co Inc
Plastiflex Healthcare Division of Plastiflex Group NV Plastiflex Healthcare Division of
Plastiflex Group NV
Heuer International (A division of GST Corporation Limited) Heuer International A division of GST
Corporation Limited
Arrow International, Inc. (subsidiary of Teleflex, Inc.) Arrow International Inc subsidiary of
Teleflex Inc
Filtertek, B.V. (An ITW Medical Company) Filtertek BV An ITW Medical Company
Fitz c/o YBI Fitz co YBI
我的预期输出是:
Original_Input Cleansed_Input
Iris Diagnostics, a Division of Iris International Inc Iris Diagnostics a Division of Iris International Inc
GINGI-PAK a division of The Belport Co., Inc. GINGIPAK a division of The Belport Co Inc
Plastiflex Healthcare Division of Plastiflex Group NV Plastiflex Healthcare Division of Plastiflex Group NV
Heuer International (A division of GST Corporation Limited) Heuer International A division of GST Corporation Limited
Arrow International, Inc. (subsidiary of Teleflex, Inc.) Arrow International Inc subsidiary of Teleflex Inc
Filtertek, B.V. (An ITW Medical Company) Filtertek BV An ITW Medical Company
Fitz c/o YBI Fitz co YBI
Parent_company
Iris Diagnostics
GINGIPAK
Plastiflex Healthcare
Heuer International
Arrow International Inc
Filtertek BV
Fitz
所以'A division of', 'Division of', '(a', '(an', 'subsidiary of', 'C/O' 之前的字符串或单词应该作为Parent_company。
我使用的一段代码如下:
data['Parent_Company'] = re.sub('A division of','',str(data['Cleansed_Input']))
我没有得到所需的输出。我希望这些定界符中的字符串结束,并且只放置公司名称,或者希望将这些定界符之前的名称拆分并放置为 Parent_company。
在此先感谢您的帮助。
解决方案
您可以使用正则表达式执行此操作。像这样的东西应该工作:
import pandas as pd
import re
def get_parent_company(input):
keywords = ["a division of", "co", "subsidiary of", "division of","an"]
regex = r"(.*?)(\b{}\b)".format("\\b|\\b".join(keywords))
matches = re.finditer(regex, input, re.IGNORECASE)
for match in matches:
return match.group(1).strip()
df["Parent_Company"] = df["Cleansed_Input"].apply(get_parent_company)
print(df)
输出:
Cleansed_Input Parent_Company
0 Iris Diagnostics a Division of Iris Internatio... Iris Diagnostics
1 GINGIPAK a division of The Belport Co Inc GINGIPAK
2 Plastiflex Healthcare Division of Plastiflex G... Plastiflex Healthcare
3 Heuer International A division of GST Corporat... Heuer International
4 Arrow International Inc subsidiary of Teleflex... Arrow International Inc
5 Filtertek BV An ITW Medical Company Filtertek BV
6 Fitz co YBI Fitz
解释
最终的正则表达式如下所示:
(.*?)(\ba division of\b|\bco\b|\bsubsidiary of\b|\bdivision of\b|\ban\b)
(.*?)
是我们想要的捕获组。这意味着所有字符.*
,但只表示尽可能少的次数?
。这是必需的,以便它匹配第一次出现。否则我们的比赛
GINGIPAK a division of The Belport Co Inc
将会
GINGIPAK a division of The Belport
因为最后一个匹配是Co
which 也是我们的关键字之一,但我们想匹配第一个a division of
。
其余的都是我们想要的所有关键字,|
以便它匹配其中的任何一个。我们在\b
之前和之后放置,以便它匹配确切的单词,否则匹配
Heuer International A division of GST Corporation Limited
将会
Heuer International A division of GST
因为在 Corporation 中我们有 co,但我们只想将 co 作为一个整体进行匹配。
最后,我们采用第一个匹配项match.group(1).strip()
并(.*?)
删除结尾空格。
推荐阅读
- sql - 如何在 oracle 中的十进制列中插入空值或空值?
- bixby - 在 Bixby Studio 中未学习草稿培训是什么意思?
- python - 在 Ubuntu 上以编程方式在后台“从”Python 3 执行脚本?
- javascript - 加载背景时如何加载动画
- c++ - C++ 将成员作为变量传递给 Main ()
- c# - C# 中的动态 If 语句
- c# - C# 无法打开 STMicro 虚拟 COM 端口
- ionic-framework - 当我单击一个按钮时,它会显示许多警报、堆栈和重叠
- selenium - 无法通过 Azure Devops Pipleine 在配置的 VM 中看到 Web 自动化
- amazon-web-services - AWS Amplify graphql 客户端 IAM 设置