How to get coordinates from postal codes and add them into df using a loop
Question:
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
Answers:
Hi you need to define your geocoder function and loop it on your df. I am passing your Postal code column one by one in the function and to fetch values from geocoder and assigning and storing it into two new columns latitude and longitude. See below:
import geocoder
def get_geocoder(postal_code_from_df):
# 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]
return latitude,longitude
for i in range(0,len(post_df)):
post_df['Latitude'][i],post_df['Longitude'][i]=get_geocoder(post_df.iloc[i]['Postcode'])
This should work for you.
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
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
Hi you need to define your geocoder function and loop it on your df. I am passing your Postal code column one by one in the function and to fetch values from geocoder and assigning and storing it into two new columns latitude and longitude. See below:
import geocoder
def get_geocoder(postal_code_from_df):
# 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]
return latitude,longitude
for i in range(0,len(post_df)):
post_df['Latitude'][i],post_df['Longitude'][i]=get_geocoder(post_df.iloc[i]['Postcode'])
This should work for you.
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