Skip to content Skip to sidebar Skip to footer

Reverse Geocoding For Pandas Dataframe

We have longitude and latitude data and need to transform them into zip codes for new york city. Is there any way to do with a python package for 20,000 rows?

Solution 1:

The uszipcode package can do what you're looking for.

from uszipcode import SearchEngine
search = SearchEngine(simple_zipcode=True)
from uszipcode import Zipcode
import numpy as np

def get_zipcode(lat, lon):
    result = search.by_coordinates(lat = lat, lng = lon, returns = 1)
    return result[0].zipcode

lat = np.random.uniform(35,45,10)
lon = np.random.uniform(-100, -110, 10)
df = pd.DataFrame({'lat':lat, 'lon':lon})

df['zipcode'] = df.apply(lambda x: get_zipcode(x.lat,x.lon), axis=1)
df

    lat          lon        zipcode
0   35.535132   -104.418912 88421
1   39.949551   -108.999900 81648
2   39.684619   -104.583286 80018
3   42.080516   -104.489692 82243
4   39.944844   -101.249686 67745
5   38.437412   -101.276961 67861
6   38.900596   -105.557827 80820
7   36.879532   -106.541044 87520
8   43.241656   -107.312935 82630
9   41.541356   -103.589179 69345

Post a Comment for "Reverse Geocoding For Pandas Dataframe"