Why am I not able to use SearchEngine() from uszipcodes to get city names?

650 Views Asked by At

I have a pd dataframe with zipcodes for customers and I am trying to add a new row into the dataframe that containts city obtained from uszipcodes using SearchEngine()

!pip install uszipcode
from uszipcode import SearchEngine, SimpleZipcode
import numpy as np
import pandas as pd

data = pd.read_csv(dataset)

data['City_Name'] = np.nan
search = SearchEngine()
for i in data['ZIPCode']:
    data['City_Name'] = search.by_zipcode(i).city

I get this error:

> ---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call last)
> Input In [35], in <cell line: 2>()
> 1 search = SearchEngine()
> 2 for i in data['ZIPCode']:
> 3     #city = search.by_zipcode(i).city
> 4     SearchEngine().by_zipcode(i).city
> 
> AttributeError: 'NoneType' object has no attribute 'city'

When trying this just to test:

city = search.by_zipcode(75024).city

It works fine.

Not sure what's going on, any help would be greatly appreciated!

edit: I figured out that it was failing at ZIP 92717, and proceeded to drop that row and run the same code again. now I'm getting this error.

> --------------------------------------------------------------------------- TypeError             Traceback (most recent call
> last) Input In [96], in <cell line: 4>()
>       2 search = SearchEngine()
>       3 num = 0
> ----> 4 for i in data['ZIPCode']:
>       5     data['City_Name'] = search.by_zipcode(i).city
> 
> TypeError: 'NoneType' object is not subscriptable
0

There are 0 best solutions below