Search code examples
pythonpandasloopscoordinates

How to get coordinates from postal codes and add them into df using a loop


I have the following dataframe:

d = {'Postcode': ['M3A','M4A','M5A','M6A','M9A','M1B'], 'Borough': ['North York', 'Downtown Toronto', 'Etobicoke', 
                                                                    'Scarborough', 'East York', 'York'], 
     'Neighbourhood': ['Parkwoods', 'Victoria Village', 'Harbourfront', 'Regent Park',
       'Lawrence Heights', 'Lawrence Manor']}
post_df = pd.DataFrame(data = d)

Which yields something like:

    Postcode    Borough             Neighbourhood
0   M3A         North York          Parkwoods
1   M4A         Downtown Toronto    Victoria Village
2   M5A         Etobicoke           Harbourfront
3   M6A         Scarborough         Regent Park
4   M9A         East York           Lawrence Heights
5   M1B         York                Lawrence Manor

I want to get all the latitudes and longitudes for each postal code. I figured out this code to do so:

import geocoder

# initialize your variable to None
lat_lng_coords = None

# loop until you get the coordinates
while(lat_lng_coords is None):
  g = geocoder.google('{}, Toronto, Ontario'.format(postal_code_from_df))
  lat_lng_coords = g.latlng

latitude = lat_lng_coords[0]
longitude = lat_lng_coords[1]

now my question is: Using the previous code, I would like to get each latitude and longitude for each postal code and add them to 2 new columns in this existing df called 'Latitude' and 'Longitude'. How could I do that using a single loop to avoid searching each postal code coordinates one by one?

Thank you very much in advance


Solution

  • You can use df.apply. Something like:

    post_df['Latitude'], post_df['Longitude'] = zip(*post_df['Postcode'].apply(get_geocoder))
    

    Where get_geocoder can be defined as mentioned by @Ankur