<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
Step1:  1. Get zip code from wikipedia
Step6:  2. Convert zip code to coordinates
Step7:  3. Sanity check
Step8:  4. Get bussiness type and # of establishments per year from US census
Check US census for the data. It can be downloaded as csv format.
Step9:  3. Collect property values per zip code over time
Step12:  Neighborhood boundaries in SF
<ASSISTANT_TASK:>
Python Code:
 GET SF ZIP CODES from http://www.city-data.com/zipmaps/San-Francisco-California.html    
import itertools
sf_zip_codes = [94102, 94103, 94104, 94105, 94107, 94108, 94109, 94110, 94111, 94112, 94114, 94115, 94116, 94117, 94118, 94121, 94122, 94123, 94124, 94127, 94129, 94131, 94132, 94133, 94134, 94158]
Explanation: 1. Get zip code from wikipedia
End of explanation
 Geopy has zip code converter! 
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("78704")
print 'EXAMPLE:'
print(location.address)
print((location.latitude, location.longitude))
    But something is wrong.
location = geolocator.geocode(sf_zip_codes[0])
print 'EXAMPLE:'
print(location.address)
print((location.latitude, location.longitude))
    So we're using Google Geocode API.
GOOGLE_KEY = ''
query_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=94102&key=%s' % (GOOGLE_KEY)
r = requests.get(query_url)
r.json()
    Get coordinates.
temp = r.json()
temp_ = temp['results'][0]['geometry']['location']
temp_
lats = []
lngs = []
for sf_zip_code in sf_zip_codes:
    query_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s' % (str(sf_zip_code),GOOGLE_KEY)
    r = requests.get(query_url)    
    temp = r.json()
    lat = temp['results'][0]['geometry']['location']['lat']
    lng = temp['results'][0]['geometry']['location']['lng']
    lats.append(lat)
    lngs.append(lng)
Explanation: 2. Convert zip code to coordinates
End of explanation
import folium
m = folium.Map(location=[37.7786871, -122.4212424],zoom_start=13)
m.circle_marker(location=[37.7786871, -122.4212424],radius=100)
for i in range(len(sf_zip_codes)):
     m.circle_marker(location=[lats[i], lngs[i]], radius=500, #100 seems good enough for now
                    popup=str(sf_zip_codes[i]), line_color = "#980043",
                    fill_color="#980043", fill_opacity=.2)
m.create_map(path='sf_zip_code_map.html')
Explanation: 3. Sanity check: map visualization
End of explanation
# business type
df = pd.read_csv('zbp13detail.txt')
df.head()
sf_zip_codes = [94102, 94103, 94104, 94105, 94107, 94108, 94109, 94110, 94111, 94112, 94114, 94115, 94116, 94117, 94118, 94121, 94122, 94123, 94124, 94127, 94129, 94131, 94132, 94133, 94134, 94158]
oak_zip_codes = [94601, 94602, 94603, 94605, 94606, 94607, 94610, 94611, 94612, 94613, 94621]
bay_zip_codes = sf_zip_codes + oak_zip_codes
# save zipcode file
import csv
myfile = open('bay_zip_codes.csv', 'wb')
wr = csv.writer(myfile)
wr.writerow(bay_zip_codes)
# load zipcode file
with open('bay_zip_codes.csv', 'rb') as f:
    reader = csv.reader(f)
    bay_zip_codes = list(reader)[0]
# convert str list to int list
bay_zip_codes = map(int, bay_zip_codes)
df_sf_oak = df.loc[df['zip'].isin(bay_zip_codes)]
# save as a file
df_sf_oak.to_csv('ZCBT_sf_oak_2013.csv',encoding='utf-8',index=False)
# sf1.sort(columns='est',ascending=False)
df_sf_oak.tail()
# let's compare to EPA
epa = b.loc[b['zip'] == 94303]
epa.sort(columns='est',ascending=False)
Explanation: 4. Get bussiness type and # of establishments per year from US census
Check US census for the data. It can be downloaded as csv format.
End of explanation
import trulia.stats as trustat
import trulia.location as truloc
zip_code_stats = trulia.stats.TruliaStats(TRULIA_KEY).get_zip_code_stats(zip_code='90025', start_date='2014-01-01', end_date='2014-01-31')
temp = zip_code_stats['listingStats']['listingStat']
df = DataFrame(temp)
df.head()
def func(x,key):
    k = x['subcategory'][0][key] # here I read key values 
    return pd.Series(k)
df['numProperties']=df['listingPrice'].apply((lambda x: func(x,'numberOfProperties')))
df['medPrice']=df['listingPrice'].apply((lambda x: func(x,'medianListingPrice')))
df['avrPrice']=df['listingPrice'].apply((lambda x: func(x,'averageListingPrice')))
df = df.drop('listingPrice',1)
df.head()
Explanation: 3. Collect property values per zip code over time
End of explanation
    Get neighborhoods
neighborhoods = trulia.location.LocationInfo(TRULIA_KEY).get_neighborhoods_in_city('San Francisco', 'CA')
neighborhoods
 Trulia does not provide coordinates.
Alamo_Square = neighborhoods[0]
Alamo_Square
neighborhood_stats = trustat.TruliaStats(TRULIA_KEY).get_neighborhood_stats(neighborhood_id=7183, start_date='2012-01-01', end_date='2012-06-30')
neighborhood_stats.keys()
neighborhood_stats['listingStats'].keys()
a = neighborhood_stats['listingStats']['listingStat']
b = DataFrame(a)
b.head()
# Let's focus on All properties
x = b['listingPrice'][0]
x['subcategory'][0]
x['subcategory'][0]['type']
b['numProperties']=b['listingPrice'].apply((lambda x: func(x,'numberOfProperties')))
b['medPrice']=b['listingPrice'].apply((lambda x: func(x,'medianListingPrice')))
b['avrPrice']=b['listingPrice'].apply((lambda x: func(x,'averageListingPrice')))
b.drop('listingPrice',1)
matplotlib.dates.date2num(a)
date_list=[]
for date in b['weekEndingDate']:
    date_list.append(datetime.strptime(date,'%Y-%m-%d'))
#a = datetime.strptime(b['weekEndingDate'],'%Y-%m-%d')
# plot time vs. value
dates = matplotlib.dates.date2num(date_list)
fig, ax = plt.subplots()
ax.plot_date(dates, b.medPrice,'-')
Explanation: Neighborhood boundaries in SF
End of explanation
<END_TASK>