# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Week-3 IBM DS Professional Assignment-Part2

# Assignment Details & Submission Guidelines

# Now that you have built a dataframe of the postal code of each neighborhood along with the borough name and neighborhood name, in order to utilize the Foursquare location data, we need to get the latitude and the longitude coordinates of each neighborhood.
#
# In an older version of this course, we were leveraging the Google Maps Geocoding API to get the latitude and the longitude coordinates of each neighborhood. However, recently Google started charging for their API: http://geoawesomeness.com/developers-up-in-arms-over-google-maps-api-insane-price-hike/, so we will use the Geocoder Python package instead: https://geocoder.readthedocs.io/index.html.
#
# The problem with this Package is you have to be persistent sometimes in order to get the geographical coordinates of a given postal code. So you can make a call to get the latitude and longitude coordinates of a given postal code and the result would be None, and then make the call again and you would get the coordinates. So, in order to make sure that you get the coordinates for all of our neighborhoods, you can run a while loop for each postal code. Taking postal code M5G as an example, your code would look something like this:

# Due to problems with geocoder, Perfer to use following file:

# Given that this package can be very unreliable, in case you are not able to get the geographical coordinates of the neighborhoods using the Geocoder package, here is a link to a csv file that has the geographical coordinates of each postal code: http://cocl.us/Geospatial_data

# +
#Importing Dependencies
import numpy as np # library to handle data in a vectorized manner

import pandas as pd # library for data analsysis
print("***********Libraries Imported*********")
# -

#Getting Geospatial_data and creating data frame using pandas
url='http://cocl.us/Geospatial_data'
ll_df=pd.read_csv(url)
ll_df.head()

ll_df.shape

toronto_csv = "./df_toronto_postcodes.csv" #CSV File saved from Part1 https://github.com/shasaankdave/Coursera_Capstone/blob/master/Week3%20IBM%20DS%20Coursera%20Capstone_Segmentaion_Clustering_Neighbourhood_Toronto_Part1.ipynb
postcode_df = pd.read_csv(toronto_csv,index_col=0)
postcode_df.head()

# +
#Discrepancies between field names in ll_df & postcode_df, key = PostCode
#Rename field

ll_df.rename(columns={"Postal Code": "PostalCode"}, inplace=True)
ll_df.head()
# -

#Check Postcode dataframe before merging
postcode_df.head()

#Merge longitude latitude datafram & Postal Code dataframe on key "PostalCode"
toronto_df = postcode_df.merge(ll_df, on="PostalCode", how="left")
toronto_df.head()


toronto_df.to_csv(r'df_toronto_details.csv')



StarcoderdataJupyterScriptsDedupFiltered