<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Title
Step1:  Create some simulated geo data
Geo-data comes in a wide variety of forms, in this case we have a Python dictionary of five latitude and longitude strings, with each coordinate in a coordinate pair separated by a comma.
Step2:  While technically unnecessary, because I originally come from R, I am a big fan of dataframes, so let us turn the dictionary of simulated data into a dataframe.
Step3:  You can see now that we have a a dataframe with five rows, with each now containing a string of latitude and longitude. Before we can work with the data, we'll need to 1) seperate the strings into latitude and longitude and 2) convert them into floats. The function below does just that.
Step4:  Let's take a took a what we have now.
Step5:  Awesome. This is exactly what we want to see, one column of floats for latitude and one column of floats for longitude.
Reverse Geocoding
To reverse geocode, we feed a specific latitude and longitude pair, in this case the first row (indexed as '0') into pygeocoder's reverse_geocoder function.
Step6:  Now we can take can start pulling out the data that we want.
Step7:  Geocoding
For geocoding, we need to submit a string containing an address or location (such as a city) into the geocode function. However, not all strings are formatted in a way that Google's geo-API can make sense of them. We can text if an input is valid by using the .geocode().valid_address function.
Step8:  Because the output was True, we now know that this is a valid address and thus can print the latitude and longitude coordinates.
Step9:  But even more interesting, once the address is processed by the Google geo API, we can parse it and easily separate street numbers, street names, etc.
<ASSISTANT_TASK:>
Python Code:
# Load packages
from pygeocoder import Geocoder
import pandas as pd
import numpy as np
Explanation: Title: Geocoding And Reverse Geocoding
Slug: geocoding_and_reverse_geocoding
Summary: Geocoding And Reverse Geocoding
Date: 2016-05-01 12:00
Category: Python
Tags: Data Wrangling
Authors: Chris Albon  
Geocoding (converting a physical address or location into latitude/longitude) and reverse geocoding (converting a lat/long to a physical address or location) are common tasks when working with geo-data.
Python offers a number of packages to make the task incredibly easy. In the tutorial below, I use pygeocoder, a wrapper for Google's geo-API, to both geocode and reverse geocode.
Preliminaries
First we want to load the packages we will want to use in the script. Specifically, I am loading pygeocoder for its geo-functionality, pandas for its dataframe structures, and numpy for its missing value (np.nan) functionality.
End of explanation
# Create a dictionary of raw data
data = {'Site 1': '31.336968, -109.560959',
        'Site 2': '31.347745, -108.229963',
        'Site 3': '32.277621, -107.734724',
        'Site 4': '31.655494, -106.420484',
        'Site 5': '30.295053, -104.014528'}
Explanation: Create some simulated geo data
Geo-data comes in a wide variety of forms, in this case we have a Python dictionary of five latitude and longitude strings, with each coordinate in a coordinate pair separated by a comma.
End of explanation
# Convert the dictionary into a pandas dataframe
df = pd.DataFrame.from_dict(data, orient='index')
# View the dataframe
df
Explanation: While technically unnecessary, because I originally come from R, I am a big fan of dataframes, so let us turn the dictionary of simulated data into a dataframe.
End of explanation
# Create two lists for the loop results to be placed
lat = []
lon = []
# For each row in a varible,
for row in df[0]:
    # Try to,
    try:
        # Split the row by comma, convert to float, and append
        # everything before the comma to lat
        lat.append(float(row.split(',')[0]))
        # Split the row by comma, convert to float, and append
        # everything after the comma to lon
        lon.append(float(row.split(',')[1]))
    # But if you get an error
    except:
        # append a missing value to lat
        lat.append(np.NaN)
        # append a missing value to lon
        lon.append(np.NaN)
# Create two new columns from lat and lon
df['latitude'] = lat
df['longitude'] = lon
Explanation: You can see now that we have a a dataframe with five rows, with each now containing a string of latitude and longitude. Before we can work with the data, we'll need to 1) seperate the strings into latitude and longitude and 2) convert them into floats. The function below does just that.
End of explanation
# View the dataframe
df
Explanation: Let's take a took a what we have now.
End of explanation
# Convert longitude and latitude to a location
results = Geocoder.reverse_geocode(df['latitude'][0], df['longitude'][0])
Explanation: Awesome. This is exactly what we want to see, one column of floats for latitude and one column of floats for longitude.
Reverse Geocoding
To reverse geocode, we feed a specific latitude and longitude pair, in this case the first row (indexed as '0') into pygeocoder's reverse_geocoder function.
End of explanation
# Print the lat/long
results.coordinates
# Print the city
results.city
# Print the country
results.country
# Print the street address (if applicable)
results.street_address
# Print the admin1 level
results.administrative_area_level_1
Explanation: Now we can take can start pulling out the data that we want.
End of explanation
# Verify that an address is valid (i.e. in Google's system)
Geocoder.geocode("4207 N Washington Ave, Douglas, AZ 85607").valid_address
Explanation: Geocoding
For geocoding, we need to submit a string containing an address or location (such as a city) into the geocode function. However, not all strings are formatted in a way that Google's geo-API can make sense of them. We can text if an input is valid by using the .geocode().valid_address function.
End of explanation
# Print the lat/long
results.coordinates
Explanation: Because the output was True, we now know that this is a valid address and thus can print the latitude and longitude coordinates.
End of explanation
# Find the lat/long of a certain address
result = Geocoder.geocode("7250 South Tucson Boulevard, Tucson, AZ 85756")
# Print the street number
result.street_number
# Print the street name
result.route
Explanation: But even more interesting, once the address is processed by the Google geo API, we can parse it and easily separate street numbers, street names, etc.
End of explanation
<END_TASK>