2

How do I take addresses and generate lat, long coordinates from them in python? I have a few addresses that I would like get lat, long points but seems it doesn't work.

I used geopandas but it returns me nothing. I am also a bit confused about what to use for the user_agent. Here is my code,

import pandas as pd
from geopy.geocoders import Nominatim

df2['location_lat'] = ""
df2['location_long'] = ""

geolocator = Nominatim(user_agent="myApp")

for i in df2.index:
    try:
        #tries fetch address from geopy
        location = geolocator.geocode(df2['Location'][i])
        
        #append lat/long to column using dataframe location
        df2.loc[i,'location_lat'] = location.latitude
        df2.loc[i,'location_long'] = location.longitude
  
    except:
        #catches exception for the case where no value is returned
        #appends null value to column
        df2.loc[i,'location_lat'] = ""
        df2.loc[i,'location_long'] = ""
   

Any help is appreciated. Thanks.

Tahsin Alam
  • 147
  • 1
  • 10
  • Could you show some addresses that you are trying to find the lat, long for? – Serge Ballesta Nov 05 '21 at 15:42
  • 1) 2094 Valentine Avenue,Bronx,NY,10457. 2) 1123 East Tremont Avenue,Bronx,NY,10460. 3) 412 Macon Street,Brooklyn,NY,11233. Here are some addresses. Thanks. – Tahsin Alam Nov 05 '21 at 15:58
  • 1
    User_agent should be a rather unique string identifying your application. This could be your name, for example. The point is that it should be unique enough to ensure others don't use it. – KostyaEsmukov Nov 06 '21 at 11:48

2 Answers2

6

You can use apply directly on a DataFrame column:

import pandas as pd
from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="myApp")

df2 = pd.DataFrame({'Location':
            ['2094 Valentine Avenue,Bronx,NY,10457',
             '1123 East Tremont Avenue,Bronx,NY,10460',
             '412 Macon Street,Brooklyn,NY,11233']})

df2[['location_lat', 'location_long']] = df2['Location'].apply(
    geolocator.geocode).apply(lambda x: pd.Series(
        [x.latitude, x.longitude], index=['location_lat', 'location_long']))

It should give:

                                  Location  location_lat  location_long
0     2094 Valentine Avenue,Bronx,NY,10457     40.852905     -73.899665
1  1123 East Tremont Avenue,Bronx,NY,10460     40.840130     -73.876245
2       412 Macon Street,Brooklyn,NY,11233     40.682651     -73.934353
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Hi, I did the same but I get AttributeError: 'NoneType' object has no attribute 'latitude' Could it be that the addresses I have in my df are invalid? – Ken Russel Sy Jul 06 '22 at 23:02
  • 1
    @KenRusselSy: Indeed, I have not tested that with invalid addresses. The error message if probably caused by `geocode` returning `None` for at least one address. You should split the operation in 2 steps (first just run `geocode`) to see what exactly happen. – Serge Ballesta Jul 07 '22 at 06:21
1

i made without pandas, but i think that you can use it

Code:

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent='your_app_name')

addresses = [
            '20, Willmannstraße, Fechenheim, Ost, Frankfurt am Main, Hessen, 60386, Deutschland',
            '20, Willmannstraße, Fechenheim, Ost, Frankfurt am Main, Hessen, 60386, Deutschland',
            'Pöhlen, Schönberg, Sandesneben-Nusse, Herzogtum Lauenburg, Schleswig-Holstein, 22929, Deutschland',
            'Pöhlen, Schönberg, Sandesneben-Nusse, Herzogtum Lauenburg, Schleswig-Holstein, 22929, Deutschland',
            'Pöhlen, Schönberg, Sandesneben-Nusse, Herzogtum Lauenburg, Schleswig-Holstein, 22929, Deutschland'
]

coordinates = []

for address in addresses:
    location = geolocator.geocode(address)

    lat, lon = location.latitude, location.longitude

    coordinates.append((lat, lon))

print(coordinates)

# output:
[(50.1262402, 8.767057053362574),
 (50.1262402, 8.767057053362574),
 (53.6805214, 10.4282207),
 (53.6805214, 10.4282207),
 (53.6805214, 10.4282207)]

I hope that helps you =D