基本上,我有一个数据框架,其中一列是名称列表,另一列是以某种方式与名称相关联的URL(示例df):
Name Domain
'Apple Inc' 'https://mapquest.com/askjdnas387y1/apple-inc', 'https://linkedin.com/apple-inc/askjdnas387y1/', 'https://www.apple-inc.com/asdkjsad542/'
'Aperture Industries' 'https://www.cakewasdelicious.com/aperture/run-away/', 'https://aperture-incorporated.com/aperture/', 'https://www.buzzfeed.com/aperture/the-top-ten-most-evil-companies=will-shock-you/'
'Umbrella Corp' 'https://www.umbrella-corp.org/were-not-evil/', 'https://umbrella.org/experiment-death/', 'https://www.most-evil.org/umbrella-corps/'
我正在尝试查找包含关键字或至少与关键字部分匹配的URL,这些URL直接位于以下任一项之后:
'https://NAME.whateverthispartdoesntmatter' # ...or...
'https://www.NAME.whateverthispartdoesntmatter' # <- not a real link
现在我正在使用fuzzywuzzy
包来获得部分匹配:
fuzz.token_set_ratio(name, value)
它对于部分匹配非常有用,但是匹配不是位置相关的,所以我将得到一个完美的关键字匹配,但是它位于URL中间的某个地方,而不是我需要的:
https://www.bloomberg.com/profiles/companies/aperture-inc/0117091D
使用
explode/unnest string
、str.extract
&fuzzywuzzy
首先,我们将使用this函数将您的字符串取消到行:
然后,我们使用正则表达式找到两种模式,有或没有
www
,并从中提取名称:然后我们使用
fuzzywuzzy
过滤匹配度高于80
的行:注意我用
token_sort_ratio
而不是token_set_ratio
来捕捉umbrella
和umbrella-corp
的差异从链接答案中使用的函数:
相关问题 更多 >
编程相关推荐