【问题标题】:How can I get geopy to read addresses from a data frame?如何让 geopy 从数据框中读取地址?
【发布时间】:2021-12-29 22:49:07
【问题描述】:

我正在测试这段代码。

results = [['city', 'state', 'location_raw'], 
           ['Juneau', 'AK', """3260 HOSPITAL DR JUNEAU 99801"""], 
           ['Palmer', 'AK', """2500 SOUTH WOODWORTH LOOP PALMER 99645"""], 
           ['Anchorage', 'AK', """3200 PROVIDENCE DRIVE ANCHORAGE 99508"""]]

df = pd.DataFrame(results)
print(type(df))


from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="ryan_app")
for x in range(len(df.index)):
    try:
        location = geolocator.geocode(df['location_raw'].iloc[x])
        print(location.raw)
        df['location_lat_lng'] = location.raw
    except:
        df['location_lat_lng'] = 'can not find this one...'
        print('can not find this one...')

我不断得到这个结果。

can not find this one...
can not find this one...
can not find this one...
can not find this one...

但是,如果我在下面传入这样的地址,它似乎可以正常工作。

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="ryan_app")
for x in range(len(df.index)):
    try:
        location = geolocator.geocode("3200 PROVIDENCE DRIVE ANCHORAGE 99508")
        print(location.raw)
        df['location_lat_lng'] = location.raw
    except:
        df['location_lat_lng'] = 'can not find this one...'
        print('can not find this one...')

结果。

{'place_id': 254070826, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'way', 'osm_id': 784375677, 'boundingbox': ['61.1873261', '61.1895066', '-149.8220256', '-149.8181122'], 'lat': '61.18841865', 'lon': '-149.82059674184666', 'display_name': 'Providence Alaska Medical Center, 3200, Providence Drive, Anchorage, Alaska, 99508, Anchorage', 'class': 'building', 'type': 'hospital', 'importance': 0.5209999999999999}

我一定是遗漏了一些简单的东西,但我不确定它是什么。

【问题讨论】:

    标签: python python-3.x geopy


    【解决方案1】:

    因为你没有将第一行设置为columns

    results = [['city', 'state', 'location_raw'], 
               ['Juneau', 'AK', """3260 HOSPITAL DR JUNEAU 99801"""], 
               ['Palmer', 'AK', """2500 SOUTH WOODWORTH LOOP PALMER 99645"""], 
               ['Anchorage', 'AK', """3200 PROVIDENCE DRIVE ANCHORAGE 99508"""]]
    
    df = pd.DataFrame(results)
    print(df)
    
               0      1                                       2
    0       city  state                            location_raw
    1     Juneau     AK           3260 HOSPITAL DR JUNEAU 99801
    2     Palmer     AK  2500 SOUTH WOODWORTH LOOP PALMER 99645
    3  Anchorage     AK   3200 PROVIDENCE DRIVE ANCHORAGE 99508
    
    

    columns[0, 1, 2],而不是['city', 'state', 'location_raw'],所以无法获取df['location_raw'] 的值。

    你应该在df = pd.DataFrame(results)之后添加代码:

    headers = df.iloc[0]
    df = pd.DataFrame(df.values[1:], columns=headers)
    

    类似问题:convert-row-to-column-header-for-pandas-dataframe

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      相关资源
      最近更新 更多