0

I need help doing the following.

I have a list of Latitude and Longitude values (so two columns; one called "lat", the other called "long" that I'd like to pass to a function in order to get zip codes (aka reverse geocoding). Where I'm getting stuck is in the for loop. Here's what I have so far:

from uszipcode import ZipcodeSearchEngine import pandas as pd import numpy as np search = ZipcodeSearchEngine() res = [] df = pd.read_csv('LATLONG.csv') for index, row in df.iterrows(): res = search.by_coordinate(row["lat"], row["long"], radius=2, returns=1) 

When I run the following in the terminal, it does not give me any results. I eventually have to stop the job.

If I run the script with actual numbers, it does work when I do the following:

for zipcode in res: print(zipcode.Zipcode) 

Overall, the idea is that I can pass all the latlong values from the dataframe and get an array/list of the zip codes for those values and export it back to a CSV file. Any help will be greatly appreciated.

6
  • Break it down. Does search.by_coordinate work at all, for any coordinates? Is row["lat"] in the format/encoding/scale/unit/whatever that search wants? Commented Apr 22, 2018 at 22:12
  • Sorry I know it's a bit long. search.by_coordinate does work. I've tried it by hardcoding coordinates in my list. I just can't seem to get the for loop to work correctly. I have about 100k values in my list so I'm not sure if that's why it's taking long to run the script. This is an example of what it looks like when I hardcode it (straight from my csv file): search.by_coordinate(42.4049, -82.923, radius=2, returns=5) Commented Apr 22, 2018 at 22:18
  • Then I get the following results when I run for zipcode in res: print(zipcode.Zipcode) {"City": "Detroit", "Density": 7755.4973821989515, "HouseOfUnits": 19244, "LandArea": 5.73, "Latitude": 42.40752699999999, "Longitude": -82.9447782, "NEBoundLatitude": 42.436000899999996, "NEBoundLongitude": -82.9138969, "Population": 44439, "SWBoundLatitude": 42.38746889999999, "SWBoungLongitude": -82.96503589999998, "State": "MI", "TotalWages": 434255347.0, "WaterArea": 0.0, "Wealthy": 9771.94237044038, "Zipcode": "48224", "ZipcodeType": "Standard"} and this is where I pull the "Zipcode". Commented Apr 22, 2018 at 22:22
  • So it's maybe just slow? Print out something each time through the for loop. Commented Apr 22, 2018 at 22:24
  • Okay maybe it is then. Let me run it again and just be patient. Thanks for your help. Commented Apr 22, 2018 at 22:35

1 Answer 1

1
from uszipcode import SearchEngine, SimpleZipcode, Zipcode search = SearchEngine() for i in range(len(df)): result = search.by_coordinates(df.loc[i, 'lat'], df.loc[i, 'long'], radius=3) df.loc[i, 'zipcode'] = result[0].zipcode 

I know this is an old post but I eventually ended up using 'googlemaps reverse-geocoding API' instead of 'uszipcode' package.

While I was using the 'uszipcode' package, I was able to pull out "zipcodes" only and add it back to the df with the following code snippet above. What I noticed that's different from your code is the fact that you're using "returns" parameter in your 'search.by_coordinates'. I didn't, and just pulled the first zipcode on the list or the first 2 or so. (you see why I ended up paying up for google)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.