Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reverse geopy geocoding pandas

I have the following data frame in a Jupyter Notebook that has a list of GPS coordinates with from geopy.geocoders import Nominatim and import pandas as pd.

    stop_id     Lat         Long
0   2        53.352280  -6.263668
1   3        53.352345  -6.263758
2   4        53.352604  -6.264143
3   6        53.352783  -6.264417
4   7        53.352867  -6.264543
5   8        53.353287  -6.265152

I have been trying to add a new column populated with the corresponding addresses to the GPS coordinates.

To do this I tried

df['address'] = geolocator.reverse((df['Lat'], df['Long']))

but got the following error message:

ValueError: Must be a coordinate pair or Point.

I then created another column [LatLong]

df['LatLong'] = df[df.columns[1:]].apply(
    lambda x: ', '.join(x.dropna().astype(float).astype(str)),axis=1)

    stop_id     Lat         Long         LatLong
0   2       53.352280   -6.263668    53.35228, -6.263668
1   3       53.352345   -6.263758    53.352345, -6.263758
2   4       53.352604   -6.264143    53.352604, -6.264143
3   6       53.352783   -6.264417    53.352783, -6.264417
4   7       53.352867   -6.264543    53.352867, -6.264543
5   8       53.353287   -6.265152    53.353287, -6.265152

I then ran the the following code:

df['address'] = geolocator.reverse(df['LatLong'])

however, I just get the exact same error message.

The code I have used above is adapted from other answers on this site to similar questions and GeoPy's documentation, so I am presuming my code is not exact enough to extract the GPS coordinates in the correct way for geopy.

Can anyone point out my error to me?

like image 750
DreamingMan Avatar asked Nov 22 '25 16:11

DreamingMan


1 Answers

Problem

Your error message says:

ValueError: Must be a coordinate pair or Point

In both:

df['address'] = geolocator.reverse((df['Lat'], df['Long']))

and

df['address'] = geolocator.reverse(df['LatLong'])

you are sending a pandas structure into a method that does not understand them.

Solution

I have no way to test this, but a solution can look something like:

df['address'] = df.apply(
    lambda row: geolocator.reverse((row['Lat'], row['Long'])), axis=1)
like image 165
Stephen Rauch Avatar answered Nov 25 '25 05:11

Stephen Rauch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!