【发布时间】:2021-09-11 18:10:28
【问题描述】:
我对这个看似简单的任务有疑问。 这是我的问题的娱乐:
我有一个名为 legal 的数据框:
+----+-----------------+
| | legal |
|----+-----------------|
| 0 | gmbh |
| 1 | kg |
| 2 | ag |
| 3 | GmbH & Co. KGaA |
| 4 | LP |
| 5 | LLP |
| 6 | LLLP |
| 7 | LLC |
| 8 | PLLC |
| 9 | corp |
| 10 | corporation |
| 11 | inc |
| 12 | cic |
| 13 | cio |
| 14 | ltd |
| 15 | s.a. |
+----+-----------------+
它包含所有可以代表给定公司的法律条款的词。
现在我有另一个数据框,其中包含公司原始名称列表,其中可能还包含一些法律条款。
我的任务是为companies 数据框中的每个公司行名称识别此类法律条款。
我正在尝试使用一些正则表达式,以便法律术语可能是大写和小写(或混合)。所以我为此使用了 extract 方法。
为了演示,我的第一个公司原始名称是 2&0 Technologies Inc,因此对于该公司,我希望从我的合法数据框中提取世界 inc。
这是我的代码的简化版本,带有一些 cmets:
def format_companies(self, legals, locations):
self.companies['base_name'] = ''
self.companies['location'] = ''
self.companies['legal'] = ''
for i, row in self.companies.iterrows():
legal_pattern = '/(' + "|".join(row['raw'].split()]) +')/ig'
legal_pattern = rf'{legal_pattern}'
print(legal_pattern) # It prints out -> /(2&0|Technologies|Inc)/ig
legal = legals['legal'].str.extract(legal_pattern)
print(tabulate(legal, headers='keys', tablefmt='psql')) # Everything is NaN. (results will be print below)
if i >= 0:
break
第一个打印语句只是打印出extract方法中使用的模式,即/(2&0|Technologies|Inc)/ig。
第二个模式是打印extract方法的结果,如cmets中所说,它返回一个NaN列表:
+----+-----+
| | 0 |
|----+-----|
| 0 | nan |
| 1 | nan |
| 2 | nan |
| 3 | nan |
| 4 | nan |
| 5 | nan |
| 6 | nan |
| 7 | nan |
| 8 | nan |
| 9 | nan |
| 10 | nan |
| 11 | nan |
| 12 | nan |
| 13 | nan |
| 14 | nan |
| 15 | nan |
+----+-----+
我很困惑,因为如果您在 https://www.regextester.com/ 上的文本“inc”上尝试正则表达式 /(2&0|Technologies|Inc)/ig,inc 会被正确选择。
我做错了什么?
【问题讨论】: