如何使用python中的geocoder获取邮政编码,提供地理坐标

2024-04-23 23:47:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下函数将邮政编码转换为地理坐标,并将其附加到源数据帧(使用Bing maps api支持的地理编码器)

我的问题是,我如何修改它以实现相反的效果。我想提供经纬度,并得到邮政编码作为回报


def get_lats_longs(df): 

    lat_lng_coords = None
    # create lists to store our new lats and longs
    lats = []
    longs=[]
    #loop through our dataframe and look up the lat/long of each postal code
    for index, row in df.iterrows():
        postal_code=row[0]
        # loop until you get the coordinates
        lat_lng_coords = None
        while(lat_lng_coords is None):
            g = geocoder.bing('{}, Toronto, Ontario'.format(postal_code),key=bing_key)
            lat_lng_coords = g.latlng

        lats.append(lat_lng_coords[0])
        longs.append(lat_lng_coords[1])

    df['Latitude'] = lats
    df['Longitude'] = longs
    return df

提前谢谢


Tags: andthenoneloopdfgetcodeour