<jupyter_start><jupyter_text>Segmenting and Clustering Neighborhoods in TorontoPeer-graded Assignment on Week3 of the Applied Data Science Capstone Add Long and Lat to Dataframe using Geocoder  Prepare the environment<jupyter_code># Install the needed libraries
# urllib is a standard library
! pip install beautifulsoup4 # install beautifulsoup4
! pip install geocoder # install geocoder
# --------- Import libraries --------------
import geocoder # import geocoder
import pandas as pd<jupyter_output><empty_output><jupyter_text>Get the geocoder package<jupyter_code># ---------- Former way - Get it directly from google ---------
# # 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))
#   lat_lng_coords = g.latlng

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

# ----------- Get it from a pre-processed csv file ------------
geo_df = pd.read_csv('http://cocl.us/Geospatial_data')
geo_df.rename(columns = {'Postal Code':'PostalCode'}, inplace = True)
geo_df<jupyter_output><empty_output><jupyter_text>Get the processed dataframe from previous assignment<jupyter_code># ----------- Get the processed dataframe from previous assignment ------------
df = pd.read_csv('Canada_neighborhood.csv')
df<jupyter_output><empty_output><jupyter_text>Merge 2 dataframe into 1 - Done<jupyter_code>result = pd.merge(df, geo_df, on='PostalCode')
result[result['Borough']=='East Toronto']
# save it to another csv file
result.to_csv('Canada_neighborhood_geo.csv',index=False)
print('Dataframe saved!')<jupyter_output>Dataframe saved!
{'fraction_non_alphanumeric': 0.13971462544589774, 'fraction_numerical': 0.004161712247324614, 'mean_word_length': 4.705084745762712, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '558085', 'license': 'MIT', 'n_tokens_mistral': 547, 'n_tokens_neox': 523, 'n_words': 142, 'path': 'Segmenting and Clustering/2.Add-geo-to-dataframe.ipynb', 'repo_name': 'minhan74/Coursera_Data_Science_Capstone', 'size': '1682'}