0

I'm testing this code.

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...')

I keep getting this result.

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

However, if I pass in an address like this, below, it seems to work fine.

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...')

Result.

{'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}

I must be missing something simple here, but I'm not sure what it is.

ASH
  • 20,759
  • 19
  • 87
  • 200

1 Answers1

0

Because you didn't set the first row as 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 is [0, 1, 2], not ['city', 'state', 'location_raw'], so you can't get the value of df['location_raw'].

You should add codes after df = pd.DataFrame(results):

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

similar question: convert-row-to-column-header-for-pandas-dataframe

Geneva
  • 156
  • 2