2列之间的模糊匹配(Python) [英] fuzzy match between 2 columns (Python)

查看:357
本文介绍了2列之间的模糊匹配(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"df_combo" 的熊猫数据框,其中包含列"worker_id" "url_entrance" "company_name" .我正在尝试生成一个输出列,该列会告诉我"url_entrance" 列中的URL是否包含"company_name" 列中的任何单词.即使像Fuzzywuzzy这样的紧密匹配也可以使用.

I have a pandas dataframe called "df_combo" which contains columns "worker_id", "url_entrance", "company_name". I am trying to produce an output column that would tell me if the URLs in "url_entrance" column contains any word in "company_name" column. Even a close match like fuzzywuzzy would work.

例如,如果URL为"www.grandhotelseattle.com","company_name"为"Hotel Prestige Seattle",则绒毛比率可能在70-80之间.

For example, if the URL is "www.grandhotelseattle.com" and the "company_name" is "Hotel Prestige Seattle", then the fuzz ratio might be somewhere 70-80.

我尝试了以下脚本: >>> fuzz.ratio(df_combo ['url_entrance'],df_combo ['company_name']) 但它仅返回1个数字,即整个列的总模糊率.我希望每一行都有模糊比率,并将这些比率存储在新列中.

I have tried the following script: >>>fuzz.ratio(df_combo['url_entrance'],df_combo['company_name']) but it returns only 1 number which is the overall fuzz ratio for the whole column. I would like to have fuzz ratio for every row and store those ratios in a new column.

推荐答案

感谢大家的投入.我已经解决了我的问题! "agg3l"提供的链接很有帮助.我看到的"TypeError"是因为"url_entrance"或"company_name"在某些行中具有一些浮动类型.我使用以下脚本将两列都转换为字符串,重新运行fuzz.ratio脚本并使其正常工作!

Thanks everyone for your inputs. I have solved my problem! The link that "agg3l" provided was helpful. The "TypeError" I saw was because either the "url_entrance" or "company_name" has some floating types in certain rows. I converted both columns to string using the following scripts, re-ran the fuzz.ratio script and got it to work!

df_combo ['url_entrance'] = df_combo ['url_entrance'].astype(str) df_combo ['company_name'] = df_combo ['company_name'].astype(str)

df_combo['url_entrance']=df_combo['url_entrance'].astype(str) df_combo['company_name']=df_combo['company_name'].astype(str)

这篇关于2列之间的模糊匹配(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆