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

為{幸葍}努か 提交于 2019-12-13 03:53:55

问题


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)

Wich 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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/52450422/how-to-get-coordinates-from-postal-codes-and-add-them-into-df-using-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!