Organizing latitude and longitude into separate columns using Pandas and Geopy to geocode a list of addresses

Question:

I have set up a geocoding procedure in Python to determine the coordinates of a list of addresses in a csv file. I have it all set up and geocoding, however, I am struggling to figure out how to put the latitude and longitude into separate columns. I have created a ‘point’ column where the geocode point gets stored using a lambda as per the Geopy readme docs:

df[‘point’] = df[‘location’].apply(lambda loc: tuple(loc.point) if loc else None)

Right now the data looks like this: (27.9477595, -82.458444, 0.0)

Although it seems straight forward to me, I can’t seem to be able to put each coordinate in the example data above into separate columns. I want to be able to have a latitude column and a longitude column so it can be better translated in Folium.

Again, this is likely straight forward and hopefully someone can help me out. Conversely, I have not looked into what Folium can accept in terms of location data and only assuming I need to get it into separate coordinates. If insight into Folium makes my question moot, than that is fine as well but I am not sure.

I am expecting a column for latitude and a column for longitude instead of the consolidated data in a single column.

Current code to run the geocoding:

    tqdm.pandas()
    geocode = RateLimiter(geolocator.geocode, swallow_exceptions=True)
    df_trainers['gcode'] = df_trainers['full_address'].progress_apply(geocode)
    df_trainers['point'] = df_trainers['gcode'].apply(lambda loc: tuple(loc.point) if loc else None)
Asked By: Ryan Clarke

||

Answers:

You can use the following to turn your coordinate column in to three columns:

df_trainers[['latitude', 'longitude', 'altitude']] = pd.DataFrame(df_trainers['point'].tolist(), index=df_trainers.index)
Answered By: jprebys
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.