# Advanced Search for Company Name, Python and Fuzzy Match

**URL:** <http://forum.companieshouse.gov.uk/t/advanced-search-for-company-name-python-and-fuzzy-match/5162>\
**Category:** API Question\
**Created:** [June 13, 2022, 2:40pm UTC](http://forum.companieshouse.gov.uk/t/advanced-search-for-company-name-python-and-fuzzy-match/5162 "2022-06-13T14:40:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![anna.dyderska](http://forum.companieshouse.gov.uk/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@anna.dyderska](http://forum.companieshouse.gov.uk/u/anna.dyderska)\
**Post date:** [June 13, 2022, 2:40pm UTC](http://forum.companieshouse.gov.uk/t/advanced-search-for-company-name-python-and-fuzzy-match/5162/1 "2022-06-13T14:40:25Z")

</div>

Hi There  
I noticed that when using the advanced search for company name and using the (max) 20 suggested results, they are in quite random order.

To find the most relevant to my search, I used fuzzy match and filtered to get the best match - it worked pretty well. The code doesn’t have 600 limitation/5 minutes (I used batches for that as it was just more feasible for me).

Any suggestions, opinions - would be very grateful 🙂

on the top of typical libraries like panda, requests and json you need to install install thefuzz and python-Levenshtein.  
Also, I struggled to paste in the code properly, so apologies for that.

> import requests  
> import pandas as pd  
> api\_key=“your apikey”  
> cols=[“company\_name”,“company\_number”,“company\_status”,“registered\_office\_address.postal\_code”]
> 
> br=pd.read\_csv(‘’/path to your file.csv’')  
> (below code needs to be run manually for each batch if using batches - CH limitation is 600 calls per 5 minutes)  
> br=im.query(“Batches==1”)

> for index, row in br.iterrows():  
> df1=pd.DataFrame(columns=cols)  
> qstr=row[‘AccountName’]  
> uniqueID=row[‘Unique\_ID’]  
> url=“[https://api.company-information.service.gov.uk/advanced-search/companies?company\_name\_includes=](https://api.company-information.service.gov.uk/advanced-search/companies?company_name_includes=)”+ qstr  
> params={‘company\_status’:‘active’}  
> response = requests.get(url, params=params,auth=(api\_key,‘’))  
> statusCode=response.status\_code  
> if statusCode == 200:  
> responseJson=response.json()  
> df1=pd.json\_normalize(responseJson,record\_path=[‘items’])  
> df1=df1.filter([“company\_name”,“company\_number”,“company\_status”,“registered\_office\_address.postal\_code”])  
> df1=df1.assign(uniquePartyID=uniqueID,partyName=qstr,response\_code=statusCode,fuzz=0)  
> for index, row in df1.iterrows():  
> companyN=row[‘company\_name’]  
> from thefuzz import fuzz  
> fuzz=fuzz.token\_sort\_ratio(qstr,companyN)  
> df1.at[index,‘fuzz’]=fuzz  
> df2=df1.sort\_values(by=[‘fuzz’], ascending=False).head(1)  
> else:  
> continue  
> frames=[df2,df3]  
> df3=pd.concat(frames)  
> df3=df3.reset\_index()

> df3.to\_csv(‘/path to your file.csv’,index=False)
