Reverse geocoding for pandas DataFrame

后端 未结 1 460
Happy的楠姐
Happy的楠姐 2021-01-23 05:57

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?

1条回答
  •  时光取名叫无心
    2021-01-23 07:03

    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
    

    0 讨论(0)
提交回复
热议问题