Clean zip code data by convertin shapefiles


In [9]:
import requests
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import csv
import seaborn as sns

font = {'family' : 'Arial',
        'weight' : 'bold',
        'size'   : 25}
matplotlib.rc('font', **font)

Summary

  1. Get the shapefiles: easy
  2. Select cities (50 largest cities): normal
  3. Get the business data: easy
  4. Select columns: difficult
  5. Get the census data (2010, 2011, 2012, 2013): easy

1. Get the shapefiles by zip code

US census provides shapefiles for every zip code in the nation. Typically, many people use this website. However, US census file is very big, and the website cannot handle it. This page provides solutions:

  1. Install gdal
  2. Run $ ogr2ogr -f GeoJSON -t_srs crs:84 [name].geojson [name].shp where [name] is the name of the file. Make sure that you have all the unzipped shapefiles with [name].shp in the same folder.

In [10]:
# Check if this can be rendered
import folium
m = folium.Map(location=[37.769959, -122.448679], zoom_start=9)
geo_path = 'zip.geojson'
m.geo_json(geo_path= geo_path)
m.create_map('zip.html')

2. Get the city zip codes

  1. Get the list of major cities.
  2. Get the name

In [11]:
# list of major 50 cities
df_ct = pd.read_csv('citi_list.csv')
df_ct.head()


Out[11]:
city state zipcodes
0 Baltimore MD 21201, 21202, 21205, 21206, 21207, 21208, 2120...
1 Boston MA 02108, 02109, 02110, 02111, 02113, 02114, 0211...
2 Chicago IL 60018, 60068, 60176, 60601, 60602, 60603, 6060...
3 Detroit MI 48201, 48202, 48203, 48204, 48205, 48206, 4820...
4 Los Angeles CA 90003, 90004, 90005, 90006, 90008, 90007, 9001...

In [12]:
# There are many zip codes in a city, so we split the zipcodes and replicate the rows
df_city = pd.concat([pd.DataFrame([df_ct['city'].values[i]]*len(df_ct['zipcodes'][i].split(', '))) for i in range(len(df_ct))],ignore_index=True)
df_state = pd.concat([pd.DataFrame([df_ct['state'].values[i]]*len(df_ct['zipcodes'][i].split(', '))) for i in range(len(df_ct))],ignore_index=True)
df_zipcodes = pd.concat([pd.DataFrame(df_ct['zipcodes'][i].split(', ')) for i in range(len(df_ct))],ignore_index=True)

In [13]:
# put them together and name the columns
df_city_zip = pd.concat([df_city,df_state,df_zipcodes],axis=1)
df_city_zip.columns = df_ct.columns

In [14]:
df_city_zip.head()


Out[14]:
city state zipcodes
0 Baltimore MD 21201
1 Baltimore MD 21202
2 Baltimore MD 21205
3 Baltimore MD 21206
4 Baltimore MD 21207

In [17]:
df_city_zip.to_csv('city_zip_df.csv',encoding='utf-8',index=False)

3. Sanity check: compare the zip code list w/ ZCTA zipcodes


In [18]:
# open zip code file
import json
with open('zip.geojson') as f:
    data = json.load(f)

In [19]:
# Get shapfile zipcode list
zcta_zipcodes = []
for i in range(len(data['features'])):
    zcta_zipcodes.append(str(data['features'][i]['properties']['ZCTA5CE10']))

In [20]:
# Get the zipcode, which only exists in ZCTA
zipcodes_filtered = []
for zipcode in df_city_zip.zipcodes:
    if zipcode in zcta_zipcodes:
        zipcodes_filtered.append(zipcode)

4. Trim the geojson file to get the selected cities


In [21]:
""" 
    'data' has a key 'feature' which has n rows, 
    each of which has 'properties' key which has 'ZCTA5CE10'
"""
df_feat = pd.DataFrame(data['features'])

In [22]:
df_feat.head()


Out[22]:
geometry properties type
0 {u'type': u'Polygon', u'coordinates': [[[-87.3... {u'AWATER10': 12694318, u'AFFGEOID10': u'86000... Feature
1 {u'type': u'Polygon', u'coordinates': [[[-110.... {u'AWATER10': 488785, u'AFFGEOID10': u'8600000... Feature
2 {u'type': u'Polygon', u'coordinates': [[[-109.... {u'AWATER10': 3082, u'AFFGEOID10': u'8600000US... Feature
3 {u'type': u'Polygon', u'coordinates': [[[-90.8... {u'AWATER10': 10549055, u'AFFGEOID10': u'86000... Feature
4 {u'type': u'Polygon', u'coordinates': [[[-118.... {u'AWATER10': 1540929, u'AFFGEOID10': u'860000... Feature

In [23]:
# create a new df (temp) for selected cities
temp = pd.DataFrame()
for i in range(len(data['features'])):
    if str(data['features'][i]['properties']['ZCTA5CE10']) in zipcodes_filtered:
        temp = pd.concat([temp,df_feat.iloc[[i]]],ignore_index=True)
len(temp)


Out[23]:
586

In [24]:
temp.head()


Out[24]:
geometry properties type
0 {u'type': u'Polygon', u'coordinates': [[[-122.... {u'AWATER10': 1299131, u'AFFGEOID10': u'860000... Feature
1 {u'type': u'Polygon', u'coordinates': [[[-77.0... {u'AWATER10': 0, u'AFFGEOID10': u'8600000US205... Feature
2 {u'type': u'Polygon', u'coordinates': [[[-73.9... {u'AWATER10': 0, u'AFFGEOID10': u'8600000US112... Feature
3 {u'type': u'Polygon', u'coordinates': [[[-74.0... {u'AWATER10': 0, u'AFFGEOID10': u'8600000US102... Feature
4 {u'type': u'Polygon', u'coordinates': [[[-73.9... {u'AWATER10': 0, u'AFFGEOID10': u'8600000US101... Feature

In [25]:
# compare the structure of the original data and the df so that we don't corrupt geojson structure
"""
    Original
"""
from pprint import pprint
pprint(data['features'][0])


{u'geometry': {u'coordinates': [[[-87.390128, 31.26047, 0.0],
                                 [-87.365618, 31.260576, 0.0],
                                 [-87.36528, 31.260364, 0.0],
                                 [-87.339969, 31.260608, 0.0],
                                 [-87.316832, 31.260845, 0.0],
                                 [-87.314535, 31.260712, 0.0],
                                 [-87.22548, 31.260639, 0.0],
                                 [-87.205088, 31.276148, 0.0],
                                 [-87.207538, 31.279942, 0.0],
                                 [-87.209069, 31.281372, 0.0],
                                 [-87.213412, 31.28365, 0.0],
                                 [-87.215381, 31.285203, 0.0],
                                 [-87.216622, 31.288821, 0.0],
                                 [-87.217267, 31.294189, 0.0],
                                 [-87.218934, 31.29895, 0.0],
                                 [-87.220058, 31.305251, 0.0],
                                 [-87.216825, 31.304509, 0.0],
                                 [-87.208359, 31.304478, 0.0],
                                 [-87.205349, 31.303944, 0.0],
                                 [-87.200272, 31.303496, 0.0],
                                 [-87.198148, 31.302717, 0.0],
                                 [-87.196157, 31.301332, 0.0],
                                 [-87.19214, 31.300709, 0.0],
                                 [-87.188676, 31.29953, 0.0],
                                 [-87.187113, 31.298399, 0.0],
                                 [-87.185053, 31.298177, 0.0],
                                 [-87.181987, 31.299646, 0.0],
                                 [-87.181513, 31.299468, 0.0],
                                 [-87.180136, 31.29949, 0.0],
                                 [-87.177845, 31.297026, 0.0],
                                 [-87.168967, 31.30357, 0.0],
                                 [-87.163413, 31.306994, 0.0],
                                 [-87.164492, 31.303718, 0.0],
                                 [-87.163949, 31.299394, 0.0],
                                 [-87.164013, 31.296672, 0.0],
                                 [-87.165839, 31.295434, 0.0],
                                 [-87.165257, 31.292811, 0.0],
                                 [-87.166718, 31.291491, 0.0],
                                 [-87.167083, 31.289698, 0.0],
                                 [-87.1683, 31.288504, 0.0],
                                 [-87.167948, 31.287608, 0.0],
                                 [-87.170581, 31.285639, 0.0],
                                 [-87.171004, 31.283912, 0.0],
                                 [-87.169371, 31.282135, 0.0],
                                 [-87.1695, 31.277003, 0.0],
                                 [-87.168847, 31.276508, 0.0],
                                 [-87.168981, 31.274435, 0.0],
                                 [-87.169763, 31.274132, 0.0],
                                 [-87.169462, 31.272383, 0.0],
                                 [-87.168239, 31.270436, 0.0],
                                 [-87.167484, 31.271041, 0.0],
                                 [-87.165755, 31.26961, 0.0],
                                 [-87.163381, 31.262256, 0.0],
                                 [-87.163618, 31.260744, 0.0],
                                 [-87.149043, 31.260772, 0.0],
                                 [-87.149089, 31.259802, 0.0],
                                 [-87.147485, 31.2558, 0.0],
                                 [-87.14654, 31.254478, 0.0],
                                 [-87.145433, 31.255879, 0.0],
                                 [-87.144024, 31.255706, 0.0],
                                 [-87.14245, 31.254352, 0.0],
                                 [-87.141985, 31.253054, 0.0],
                                 [-87.140757, 31.252929, 0.0],
                                 [-87.142448, 31.249937, 0.0],
                                 [-87.144397, 31.249112, 0.0],
                                 [-87.145972, 31.247408, 0.0],
                                 [-87.148124, 31.245997, 0.0],
                                 [-87.149557, 31.244202, 0.0],
                                 [-87.147618, 31.243779, 0.0],
                                 [-87.144313, 31.241738, 0.0],
                                 [-87.139633, 31.241926, 0.0],
                                 [-87.135466, 31.240507, 0.0],
                                 [-87.127916, 31.241134, 0.0],
                                 [-87.12897, 31.243694, 0.0],
                                 [-87.129304, 31.24586, 0.0],
                                 [-87.128347, 31.251102, 0.0],
                                 [-87.127485, 31.252108, 0.0],
                                 [-87.122558, 31.252071, 0.0],
                                 [-87.121009, 31.253572, 0.0],
                                 [-87.11661, 31.253015, 0.0],
                                 [-87.114163, 31.252122, 0.0],
                                 [-87.112504, 31.250831, 0.0],
                                 [-87.107753, 31.256127, 0.0],
                                 [-87.107797, 31.257508, 0.0],
                                 [-87.110613, 31.260935, 0.0],
                                 [-87.073739, 31.261147, 0.0],
                                 [-87.071242, 31.261009, 0.0],
                                 [-87.069232, 31.259166, 0.0],
                                 [-87.067559, 31.256355, 0.0],
                                 [-87.065781, 31.251752, 0.0],
                                 [-87.065517, 31.247003, 0.0],
                                 [-87.061085, 31.246536, 0.0],
                                 [-87.058588, 31.244755, 0.0],
                                 [-87.062641, 31.238393, 0.0],
                                 [-87.065223, 31.231794, 0.0],
                                 [-87.06506, 31.225838, 0.0],
                                 [-87.064174, 31.224061, 0.0],
                                 [-87.05637, 31.213935, 0.0],
                                 [-87.051169, 31.214162, 0.0],
                                 [-87.049549, 31.215209, 0.0],
                                 [-87.046882, 31.219491, 0.0],
                                 [-87.045894, 31.220272, 0.0],
                                 [-87.043822, 31.220481, 0.0],
                                 [-87.0423, 31.221598, 0.0],
                                 [-87.039722, 31.225227, 0.0],
                                 [-87.039721, 31.229269, 0.0],
                                 [-87.03788, 31.229229, 0.0],
                                 [-87.036528, 31.228212, 0.0],
                                 [-87.035398, 31.22611, 0.0],
                                 [-87.032636, 31.223204, 0.0],
                                 [-87.032075, 31.225051, 0.0],
                                 [-87.031134, 31.225651, 0.0],
                                 [-87.030548, 31.228474, 0.0],
                                 [-87.028691, 31.226349, 0.0],
                                 [-87.026018, 31.225455, 0.0],
                                 [-87.024272, 31.224177, 0.0],
                                 [-87.024848, 31.22165, 0.0],
                                 [-87.024469, 31.220332, 0.0],
                                 [-87.026928, 31.218663, 0.0],
                                 [-87.026927, 31.214791, 0.0],
                                 [-87.026186, 31.213795, 0.0],
                                 [-87.026894, 31.21076, 0.0],
                                 [-87.026365, 31.209258, 0.0],
                                 [-87.027133, 31.208456, 0.0],
                                 [-87.026623, 31.207041, 0.0],
                                 [-87.027199, 31.20438, 0.0],
                                 [-87.028933, 31.20477, 0.0],
                                 [-87.030588, 31.20393, 0.0],
                                 [-87.029624, 31.202023, 0.0],
                                 [-87.030707, 31.201112, 0.0],
                                 [-87.028422, 31.198708, 0.0],
                                 [-87.029419, 31.197024, 0.0],
                                 [-87.027144, 31.194909, 0.0],
                                 [-87.026843, 31.192469, 0.0],
                                 [-87.025359, 31.192383, 0.0],
                                 [-87.025785, 31.194995, 0.0],
                                 [-87.024264, 31.197791, 0.0],
                                 [-87.024104, 31.200106, 0.0],
                                 [-87.023224, 31.201275, 0.0],
                                 [-87.02173, 31.202626, 0.0],
                                 [-87.017063, 31.202969, 0.0],
                                 [-87.013409, 31.205145, 0.0],
                                 [-87.010503, 31.205212, 0.0],
                                 [-87.007651, 31.203927, 0.0],
                                 [-87.005384, 31.204247, 0.0],
                                 [-87.003622, 31.207203, 0.0],
                                 [-87.001408, 31.208991, 0.0],
                                 [-86.99997, 31.209265, 0.0],
                                 [-86.993305, 31.211559, 0.0],
                                 [-86.990719, 31.211514, 0.0],
                                 [-86.991064, 31.20952, 0.0],
                                 [-86.990343, 31.206633, 0.0],
                                 [-86.990901, 31.203172, 0.0],
                                 [-86.988685, 31.200331, 0.0],
                                 [-86.985725, 31.200103, 0.0],
                                 [-86.983219, 31.199003, 0.0],
                                 [-86.983458, 31.197949, 0.0],
                                 [-86.981245, 31.198202, 0.0],
                                 [-86.976259, 31.197218, 0.0],
                                 [-86.974019, 31.194606, 0.0],
                                 [-86.970152, 31.191467, 0.0],
                                 [-86.970125, 31.18874, 0.0],
                                 [-86.969058, 31.186929, 0.0],
                                 [-86.966497, 31.18512, 0.0],
                                 [-86.9655, 31.182402, 0.0],
                                 [-86.963555, 31.182296, 0.0],
                                 [-86.963417, 31.179498, 0.0],
                                 [-86.955388, 31.179173, 0.0],
                                 [-86.955325, 31.179971, 0.0],
                                 [-86.95179, 31.181029, 0.0],
                                 [-86.948178, 31.178639, 0.0],
                                 [-86.943805, 31.178784, 0.0],
                                 [-86.942533, 31.178097, 0.0],
                                 [-86.938238, 31.179216, 0.0],
                                 [-86.936736, 31.180944, 0.0],
                                 [-86.936066, 31.183618, 0.0],
                                 [-86.933433, 31.185088, 0.0],
                                 [-86.932887, 31.187249, 0.0],
                                 [-86.930992, 31.188691, 0.0],
                                 [-86.931057, 31.193355, 0.0],
                                 [-86.929159, 31.195765, 0.0],
                                 [-86.927433, 31.197097, 0.0],
                                 [-86.927893, 31.198395, 0.0],
                                 [-86.927364, 31.202085, 0.0],
                                 [-86.924954, 31.203307, 0.0],
                                 [-86.922908, 31.205035, 0.0],
                                 [-86.921623, 31.205184, 0.0],
                                 [-86.920453, 31.206564, 0.0],
                                 [-86.918682, 31.207346, 0.0],
                                 [-86.916815, 31.206736, 0.0],
                                 [-86.914501, 31.207424, 0.0],
                                 [-86.914092, 31.208315, 0.0],
                                 [-86.910537, 31.21068, 0.0],
                                 [-86.908306, 31.213782, 0.0],
                                 [-86.908344, 31.215828, 0.0],
                                 [-86.905506, 31.219816, 0.0],
                                 [-86.903664, 31.22295, 0.0],
                                 [-86.901509, 31.223269, 0.0],
                                 [-86.898823, 31.222411, 0.0],
                                 [-86.897153, 31.222814, 0.0],
                                 [-86.897547, 31.223617, 0.0],
                                 [-86.897036, 31.226324, 0.0],
                                 [-86.894448, 31.22827, 0.0],
                                 [-86.892794, 31.23047, 0.0],
                                 [-86.891006, 31.231775, 0.0],
                                 [-86.889058, 31.233998, 0.0],
                                 [-86.887856, 31.237252, 0.0],
                                 [-86.887882, 31.238466, 0.0],
                                 [-86.886306, 31.24227, 0.0],
                                 [-86.886599, 31.243989, 0.0],
                                 [-86.888384, 31.24697, 0.0],
                                 [-86.887264, 31.247244, 0.0],
                                 [-86.881449, 31.246921, 0.0],
                                 [-86.876566, 31.249417, 0.0],
                                 [-86.873526, 31.249898, 0.0],
                                 [-86.870646, 31.250816, 0.0],
                                 [-86.87057, 31.258241, 0.0],
                                 [-86.871022, 31.261902, 0.0],
                                 [-86.763961, 31.261293, 0.0],
                                 [-86.765568, 31.2593, 0.0],
                                 [-86.765764, 31.256555, 0.0],
                                 [-86.767056, 31.253855, 0.0],
                                 [-86.772263, 31.250655, 0.0],
                                 [-86.771144, 31.246696, 0.0],
                                 [-86.772372, 31.244634, 0.0],
                                 [-86.772592, 31.242588, 0.0],
                                 [-86.770853, 31.241562, 0.0],
                                 [-86.769467, 31.239636, 0.0],
                                 [-86.768828, 31.237321, 0.0],
                                 [-86.768802, 31.232142, 0.0],
                                 [-86.769113, 31.229869, 0.0],
                                 [-86.768107, 31.227233, 0.0],
                                 [-86.764539, 31.224028, 0.0],
                                 [-86.763569, 31.221751, 0.0],
                                 [-86.763763, 31.220463, 0.0],
                                 [-86.766823, 31.216745, 0.0],
                                 [-86.771161, 31.213211, 0.0],
                                 [-86.772592, 31.210657, 0.0],
                                 [-86.769119, 31.208796, 0.0],
                                 [-86.765893, 31.208388, 0.0],
                                 [-86.765327, 31.207015, 0.0],
                                 [-86.768459, 31.205076, 0.0],
                                 [-86.769758, 31.20366, 0.0],
                                 [-86.771962, 31.203138, 0.0],
                                 [-86.772519, 31.202243, 0.0],
                                 [-86.769524, 31.198285, 0.0],
                                 [-86.767459, 31.196619, 0.0],
                                 [-86.763513, 31.197534, 0.0],
                                 [-86.762899, 31.196915, 0.0],
                                 [-86.765773, 31.192627, 0.0],
                                 [-86.76465, 31.191717, 0.0],
                                 [-86.762197, 31.191877, 0.0],
                                 [-86.762, 31.190294, 0.0],
                                 [-86.763067, 31.188534, 0.0],
                                 [-86.762702, 31.186977, 0.0],
                                 [-86.764738, 31.184567, 0.0],
                                 [-86.766903, 31.183631, 0.0],
                                 [-86.766368, 31.182297, 0.0],
                                 [-86.763512, 31.181133, 0.0],
                                 [-86.763473, 31.179411, 0.0],
                                 [-86.766467, 31.177252, 0.0],
                                 [-86.768223, 31.177422, 0.0],
                                 [-86.771811, 31.179243, 0.0],
                                 [-86.773318, 31.1785, 0.0],
                                 [-86.773443, 31.171131, 0.0],
                                 [-86.774044, 31.170605, 0.0],
                                 [-86.776117, 31.170646, 0.0],
                                 [-86.778049, 31.170047, 0.0],
                                 [-86.781959, 31.167408, 0.0],
                                 [-86.78526, 31.16669, 0.0],
                                 [-86.788863, 31.165145, 0.0],
                                 [-86.790506, 31.165146, 0.0],
                                 [-86.79464, 31.167683, 0.0],
                                 [-86.795835, 31.167708, 0.0],
                                 [-86.798331, 31.166512, 0.0],
                                 [-86.79857, 31.164935, 0.0],
                                 [-86.800556, 31.16168, 0.0],
                                 [-86.801323, 31.15934, 0.0],
                                 [-86.800918, 31.157021, 0.0],
                                 [-86.798932, 31.155302, 0.0],
                                 [-86.79621, 31.153574, 0.0],
                                 [-86.794317, 31.151257, 0.0],
                                 [-86.793577, 31.149209, 0.0],
                                 [-86.792996, 31.144962, 0.0],
                                 [-86.792999, 31.142129, 0.0],
                                 [-86.793843, 31.13686, 0.0],
                                 [-86.794466, 31.135443, 0.0],
                                 [-86.796726, 31.135255, 0.0],
                                 [-86.800775, 31.136397, 0.0],
                                 [-86.805221, 31.139929, 0.0],
                                 [-86.80728, 31.140964, 0.0],
                                 [-86.81198, 31.141272, 0.0],
                                 [-86.816441, 31.141171, 0.0],
                                 [-86.818028, 31.140832, 0.0],
                                 [-86.818044, 31.138209, 0.0],
                                 [-86.816975, 31.135398, 0.0],
                                 [-86.819032, 31.132182, 0.0],
                                 [-86.820154, 31.131745, 0.0],
                                 [-86.821746, 31.131981, 0.0],
                                 [-86.823483, 31.132956, 0.0],
                                 [-86.823939, 31.135356, 0.0],
                                 [-86.825311, 31.136889, 0.0],
                                 [-86.829379, 31.138078, 0.0],
                                 [-86.835659, 31.137041, 0.0],
                                 [-86.838186, 31.13569, 0.0],
                                 [-86.8407, 31.135842, 0.0],
                                 [-86.843894, 31.1373, 0.0],
                                 [-86.84784, 31.137756, 0.0],
                                 [-86.850379, 31.138822, 0.0],
                                 [-86.852022, 31.138527, 0.0],
                                 [-86.854084, 31.136319, 0.0],
                                 [-86.854642, 31.133672, 0.0],
                                 [-86.853586, 31.132416, 0.0],
                                 [-86.851525, 31.131651, 0.0],
                                 [-86.849417, 31.128798, 0.0],
                                 [-86.846401, 31.127409, 0.0],
                                 [-86.845752, 31.126222, 0.0],
                                 [-86.84615, 31.125176, 0.0],
                                 [-86.850749, 31.125425, 0.0],
                                 [-86.852972, 31.125855, 0.0],
                                 [-86.858721, 31.127595, 0.0],
                                 [-86.860681, 31.127367, 0.0],
                                 [-86.862125, 31.12502, 0.0],
                                 [-86.863597, 31.120718, 0.0],
                                 [-86.865343, 31.119589, 0.0],
                                 [-86.869367, 31.118451, 0.0],
                                 [-86.871282, 31.11878, 0.0],
                                 [-86.872295, 31.12033, 0.0],
                                 [-86.871574, 31.121991, 0.0],
                                 [-86.86785, 31.123317, 0.0],
                                 [-86.868427, 31.125187, 0.0],
                                 [-86.871357, 31.125945, 0.0],
                                 [-86.873756, 31.123612, 0.0],
                                 [-86.875597, 31.122499, 0.0],
                                 [-86.878232, 31.118206, 0.0],
                                 [-86.880805, 31.11594, 0.0],
                                 [-86.885456, 31.115684, 0.0],
                                 [-86.886145, 31.114915, 0.0],
                                 [-86.885273, 31.114065, 0.0],
                                 [-86.882672, 31.112909, 0.0],
                                 [-86.882487, 31.111921, 0.0],
                                 [-86.884186, 31.110984, 0.0],
                                 [-86.886522, 31.110832, 0.0],
                                 [-86.890924, 31.10402, 0.0],
                                 [-86.891168, 31.102777, 0.0],
                                 [-86.890515, 31.099667, 0.0],
                                 [-86.890808, 31.098876, 0.0],
                                 [-86.894508, 31.098157, 0.0],
                                 [-86.895768, 31.096779, 0.0],
                                 [-86.899367, 31.095914, 0.0],
                                 [-86.901505, 31.091908, 0.0],
                                 [-86.899876, 31.089751, 0.0],
                                 [-86.898381, 31.089118, 0.0],
                                 [-86.898112, 31.090202, 0.0],
                                 [-86.896003, 31.090405, 0.0],
                                 [-86.894354, 31.088721, 0.0],
                                 [-86.893838, 31.08629, 0.0],
                                 [-86.894784, 31.084783, 0.0],
                                 [-86.890274, 31.081294, 0.0],
                                 [-86.887545, 31.079791, 0.0],
                                 [-86.886478, 31.083489, 0.0],
                                 [-86.8853, 31.086182, 0.0],
                                 [-86.882539, 31.08886, 0.0],
                                 [-86.880938, 31.08949, 0.0],
                                 [-86.875848, 31.090036, 0.0],
                                 [-86.873105, 31.090626, 0.0],
                                 [-86.867549, 31.093089, 0.0],
                                 [-86.865636, 31.093585, 0.0],
                                 [-86.860501, 31.094193, 0.0],
                                 [-86.852019, 31.092831, 0.0],
                                 [-86.849644, 31.091439, 0.0],
                                 [-86.848313, 31.089987, 0.0],
                                 [-86.846218, 31.088729, 0.0],
                                 [-86.842349, 31.087722, 0.0],
                                 [-86.837137, 31.085403, 0.0],
                                 [-86.833683, 31.082926, 0.0],
                                 [-86.831609, 31.080875, 0.0],
                                 [-86.825056, 31.075924, 0.0],
                                 [-86.819805, 31.071125, 0.0],
                                 [-86.817984, 31.07005, 0.0],
                                 [-86.814499, 31.068824, 0.0],
                                 [-86.813317, 31.067531, 0.0],
                                 [-86.811558, 31.06384, 0.0],
                                 [-86.809666, 31.061204, 0.0],
                                 [-86.809345, 31.059645, 0.0],
                                 [-86.811182, 31.059187, 0.0],
                                 [-86.81632, 31.060241, 0.0],
                                 [-86.81861, 31.06031, 0.0],
                                 [-86.821405, 31.059576, 0.0],
                                 [-86.824653, 31.056895, 0.0],
                                 [-86.826198, 31.058429, 0.0],
                                 [-86.829259, 31.060057, 0.0],
                                 [-86.830776, 31.059391, 0.0],
                                 [-86.832028, 31.057144, 0.0],
                                 [-86.833599, 31.056365, 0.0],
                                 [-86.835359, 31.056588, 0.0],
                                 [-86.832449, 31.054283, 0.0],
                                 [-86.830522, 31.053721, 0.0],
                                 [-86.829284, 31.054252, 0.0],
                                 [-86.824515, 31.054282, 0.0],
                                 [-86.820753, 31.054015, 0.0],
                                 [-86.819632, 31.053499, 0.0],
                                 [-86.819976, 31.052129, 0.0],
                                 [-86.821112, 31.051876, 0.0],
                                 [-86.819967, 31.047957, 0.0],
                                 [-86.821218, 31.047063, 0.0],
                                 [-86.820206, 31.04555, 0.0],
                                 [-86.820658, 31.043992, 0.0],
                                 [-86.817038, 31.043854, 0.0],
                                 [-86.818076, 31.042594, 0.0],
                                 [-86.820418, 31.038331, 0.0],
                                 [-86.820977, 31.036474, 0.0],
                                 [-86.822041, 31.030149, 0.0],
                                 [-86.823264, 31.025153, 0.0],
                                 [-86.823109, 31.02382, 0.0],
                                 [-86.824738, 31.024587, 0.0],
                                 [-86.827098, 31.024285, 0.0],
                                 [-86.830002, 31.021524, 0.0],
                                 [-86.840307, 31.018466, 0.0],
                                 [-86.846174, 31.013854, 0.0],
                                 [-86.845673, 31.00699, 0.0],
                                 [-86.845806, 31.004275, 0.0],
                                 [-86.846651, 31.002323, 0.0],
                                 [-86.842989, 31.003395, 0.0],
                                 [-86.841268, 31.003187, 0.0],
                                 [-86.839216, 31.00174, 0.0],
                                 [-86.833419, 30.998574, 0.0],
                                 [-86.83193301949868,
                                  30.997378015693492,
                                  0.0],
                                 [-86.831934, 30.997378, 0.0],
                                 [-86.872989, 30.997631, 0.0],
                                 [-86.888135, 30.997577, 0.0],
                                 [-86.90625784631519,
                                  30.997635011379487,
                                  0.0],
                                 [-86.92781, 30.997704, 0.0],
                                 [-86.998477, 30.998661, 0.0],
                                 [-87.004359, 30.999316, 0.0],
                                 [-87.027107, 30.999255, 0.0],
                                 [-87.036366, 30.999348, 0.0],
                                 [-87.039989, 30.999594, 0.0],
                                 [-87.053737, 30.999131, 0.0],
                                 [-87.064063, 30.999191, 0.0],
                                 [-87.068633, 30.999143, 0.0],
                                 [-87.118873, 30.999427, 0.0],
                                 [-87.124969, 30.998802, 0.0],
                                 [-87.140755, 30.999532, 0.0],
                                 [-87.162614, 30.999055, 0.0],
                                 [-87.16308332536309,
                                  30.99904075375589,
                                  0.0],
                                 [-87.16408379919267,
                                  30.999010384644286,
                                  0.0],
                                 [-87.164748, 31.0017, 0.0],
                                 [-87.164398, 31.003224, 0.0],
                                 [-87.16035, 31.008503, 0.0],
                                 [-87.158528, 31.007906, 0.0],
                                 [-87.156312, 31.00798, 0.0],
                                 [-87.150681, 31.009714, 0.0],
                                 [-87.148963, 31.012065, 0.0],
                                 [-87.144388, 31.012538, 0.0],
                                 [-87.142971, 31.01308, 0.0],
                                 [-87.141982, 31.014575, 0.0],
                                 [-87.142354, 31.015573, 0.0],
                                 [-87.145262, 31.016134, 0.0],
                                 [-87.145826, 31.016752, 0.0],
                                 [-87.145119, 31.018257, 0.0],
                                 [-87.141782, 31.01976, 0.0],
                                 [-87.13949, 31.021845, 0.0],
                                 [-87.135391, 31.023603, 0.0],
                                 [-87.13414, 31.025345, 0.0],
                                 [-87.134106, 31.027541, 0.0],
                                 [-87.135579, 31.029555, 0.0],
                                 [-87.135415, 31.030588, 0.0],
                                 [-87.133753, 31.031575, 0.0],
                                 [-87.131195, 31.032116, 0.0],
                                 [-87.130479, 31.035009, 0.0],
                                 [-87.128868, 31.036653, 0.0],
                                 [-87.126344, 31.037769, 0.0],
                                 [-87.127673, 31.037747, 0.0],
                                 [-87.130549, 31.035966, 0.0],
                                 [-87.131679, 31.036005, 0.0],
                                 [-87.133424, 31.034274, 0.0],
                                 [-87.136279, 31.034131, 0.0],
                                 [-87.139481, 31.038323, 0.0],
                                 [-87.14004, 31.036627, 0.0],
                                 [-87.139166, 31.035218, 0.0],
                                 [-87.142356, 31.035118, 0.0],
                                 [-87.149729, 31.034407, 0.0],
                                 [-87.151661, 31.03452, 0.0],
                                 [-87.155514, 31.033285, 0.0],
                                 [-87.15701, 31.029479, 0.0],
                                 [-87.159206, 31.032054, 0.0],
                                 [-87.162649, 31.033496, 0.0],
                                 [-87.16497, 31.034007, 0.0],
                                 [-87.155064, 31.041217, 0.0],
                                 [-87.158781, 31.043748, 0.0],
                                 [-87.160781, 31.044304, 0.0],
                                 [-87.162716, 31.048336, 0.0],
                                 [-87.162313, 31.049563, 0.0],
                                 [-87.158894, 31.053385, 0.0],
                                 [-87.156407, 31.058445, 0.0],
                                 [-87.156102, 31.059864, 0.0],
                                 [-87.156471, 31.059901, 0.0],
                                 [-87.164777, 31.053199, 0.0],
                                 [-87.167265, 31.05142, 0.0],
                                 [-87.175249, 31.046777, 0.0],
                                 [-87.178439, 31.044713, 0.0],
                                 [-87.182546, 31.050885, 0.0],
                                 [-87.183006, 31.053536, 0.0],
                                 [-87.182331, 31.057931, 0.0],
                                 [-87.184419, 31.057475, 0.0],
                                 [-87.191474, 31.05729, 0.0],
                                 [-87.198327, 31.056245, 0.0],
                                 [-87.203287, 31.056203, 0.0],
                                 [-87.203815, 31.05542, 0.0],
                                 [-87.209513, 31.056022, 0.0],
                                 [-87.211715, 31.055059, 0.0],
                                 [-87.214729, 31.054986, 0.0],
                                 [-87.215605, 31.057725, 0.0],
                                 [-87.214129, 31.059172, 0.0],
                                 [-87.214367, 31.061883, 0.0],
                                 [-87.213255, 31.063325, 0.0],
                                 [-87.212994, 31.066113, 0.0],
                                 [-87.21458, 31.070469, 0.0],
                                 [-87.215942, 31.071976, 0.0],
                                 [-87.216179, 31.074451, 0.0],
                                 [-87.218467, 31.074462, 0.0],
                                 [-87.230213, 31.076213, 0.0],
                                 [-87.237104, 31.079274, 0.0],
                                 [-87.241669, 31.081858, 0.0],
                                 [-87.244494, 31.082483, 0.0],
                                 [-87.247972, 31.08411, 0.0],
                                 [-87.248136, 31.087826, 0.0],
                                 [-87.250202, 31.086707, 0.0],
                                 [-87.253755, 31.087709, 0.0],
                                 [-87.256712, 31.09021, 0.0],
                                 [-87.263494, 31.09292, 0.0],
                                 [-87.265687, 31.093186, 0.0],
                                 [-87.27044, 31.095252, 0.0],
                                 [-87.271549, 31.097055, 0.0],
                                 [-87.273042, 31.098002, 0.0],
                                 [-87.276191, 31.098532, 0.0],
                                 [-87.276228, 31.080046, 0.0],
                                 [-87.275829, 31.076811, 0.0],
                                 [-87.276446, 31.074861, 0.0],
                                 [-87.276726, 31.07214, 0.0],
                                 [-87.279962, 31.075722, 0.0],
                                 [-87.282391, 31.077675, 0.0],
                                 [-87.287736, 31.080838, 0.0],
                                 [-87.293864, 31.086737, 0.0],
                                 [-87.299637, 31.096087, 0.0],
                                 [-87.303421, 31.101507, 0.0],
                                 [-87.308367, 31.107473, 0.0],
                                 [-87.310158, 31.107482, 0.0],
                                 [-87.310393, 31.107182, 0.0],
                                 [-87.310376, 31.093707, 0.0],
                                 [-87.316508, 31.09157, 0.0],
                                 [-87.316644, 31.089828, 0.0],
                                 [-87.318792, 31.088855, 0.0],
                                 [-87.318795, 31.085392, 0.0],
                                 [-87.316794, 31.085377, 0.0],
                                 [-87.31895, 31.082152, 0.0],
                                 [-87.322196, 31.078232, 0.0],
                                 [-87.322942, 31.073812, 0.0],
                                 [-87.325692, 31.072875, 0.0],
                                 [-87.327816, 31.07096, 0.0],
                                 [-87.329816, 31.07099, 0.0],
                                 [-87.333841, 31.066818, 0.0],
                                 [-87.334055, 31.065999, 0.0],
                                 [-87.335225, 31.06718, 0.0],
                                 [-87.33704, 31.066961, 0.0],
                                 [-87.339008, 31.064543, 0.0],
                                 [-87.341183, 31.069508, 0.0],
                                 [-87.34261, 31.070479, 0.0],
                                 [-87.344842, 31.070654, 0.0],
                                 [-87.346509, 31.073225, 0.0],
                                 [-87.348664, 31.077787, 0.0],
                                 [-87.351714, 31.081436, 0.0],
                                 [-87.351649, 31.082962, 0.0],
                                 [-87.356382, 31.087143, 0.0],
                                 [-87.358603, 31.087531, 0.0],
                                 [-87.357159, 31.088628, 0.0],
                                 [-87.357121, 31.089817, 0.0],
                                 [-87.360609, 31.093069, 0.0],
                                 [-87.359973, 31.094143, 0.0],
                                 [-87.360234, 31.096308, 0.0],
                                 [-87.36144, 31.098273, 0.0],
                                 [-87.360887, 31.101125, 0.0],
                                 [-87.36198, 31.102041, 0.0],
                                 [-87.36222, 31.104045, 0.0],
                                 [-87.364183, 31.107619, 0.0],
                                 [-87.364596, 31.111274, 0.0],
                                 [-87.362695, 31.112271, 0.0],
                                 [-87.363547, 31.114911, 0.0],
                                 [-87.362758, 31.115711, 0.0],
                                 [-87.361667, 31.119116, 0.0],
                                 [-87.362124, 31.121889, 0.0],
                                 [-87.360898, 31.125694, 0.0],
                                 [-87.362012, 31.126833, 0.0],
                                 [-87.366404, 31.127843, 0.0],
                                 [-87.367628, 31.129169, 0.0],
                                 [-87.369787, 31.129129, 0.0],
                                 [-87.372315, 31.131943, 0.0],
                                 [-87.371025, 31.133605, 0.0],
                                 [-87.369414, 31.134003, 0.0],
                                 [-87.368494, 31.135533, 0.0],
                                 [-87.368943, 31.137481, 0.0],
                                 [-87.368062, 31.139433, 0.0],
                                 [-87.366857, 31.140448, 0.0],
                                 [-87.36757, 31.141777, 0.0],
                                 [-87.365533, 31.143713, 0.0],
                                 [-87.364824, 31.146306, 0.0],
                                 [-87.365542, 31.14722, 0.0],
                                 [-87.365349, 31.151056, 0.0],
                                 [-87.364122, 31.152579, 0.0],
                                 [-87.362619, 31.155652, 0.0],
                                 [-87.362444, 31.158162, 0.0],
                                 [-87.36162, 31.159604, 0.0],
                                 [-87.361606, 31.161315, 0.0],
                                 [-87.362719, 31.162256, 0.0],
                                 [-87.361498, 31.164035, 0.0],
                                 [-87.363345, 31.166553, 0.0],
                                 [-87.364944, 31.166728, 0.0],
                                 [-87.368346, 31.165944, 0.0],
                                 [-87.368439, 31.184726, 0.0],
                                 [-87.358715, 31.19764, 0.0],
                                 [-87.35371, 31.205627, 0.0],
                                 [-87.351978, 31.208713, 0.0],
                                 [-87.345902, 31.222661, 0.0],
                                 [-87.344819, 31.224409, 0.0],
                                 [-87.346013, 31.226484, 0.0],
                                 [-87.346309, 31.229322, 0.0],
                                 [-87.34921, 31.232644, 0.0],
                                 [-87.350438, 31.236985, 0.0],
                                 [-87.351711, 31.236983, 0.0],
                                 [-87.354844, 31.238268, 0.0],
                                 [-87.357502, 31.237342, 0.0],
                                 [-87.358113, 31.236338, 0.0],
                                 [-87.358135, 31.233251, 0.0],
                                 [-87.357201, 31.231446, 0.0],
                                 [-87.355898, 31.230413, 0.0],
                                 [-87.356979, 31.229176, 0.0],
                                 [-87.357279, 31.230618, 0.0],
                                 [-87.358468, 31.228518, 0.0],
                                 [-87.361243, 31.227156, 0.0],
                                 [-87.365175, 31.224712, 0.0],
                                 [-87.368783, 31.224118, 0.0],
                                 [-87.370734, 31.224189, 0.0],
                                 [-87.373449, 31.224968, 0.0],
                                 [-87.375664, 31.226772, 0.0],
                                 [-87.377317, 31.22659, 0.0],
                                 [-87.37729, 31.228079, 0.0],
                                 [-87.378568, 31.231403, 0.0],
                                 [-87.380724, 31.239747, 0.0],
                                 [-87.382323, 31.24142, 0.0],
                                 [-87.385791, 31.24188, 0.0],
                                 [-87.386564, 31.242728, 0.0],
                                 [-87.386189, 31.244585, 0.0],
                                 [-87.388136, 31.24651, 0.0],
                                 [-87.387388, 31.248549, 0.0],
                                 [-87.388745, 31.252494, 0.0],
                                 [-87.388877, 31.254419, 0.0],
                                 [-87.38981, 31.255863, 0.0],
                                 [-87.390128, 31.26047, 0.0]]],
               u'type': u'Polygon'},
 u'properties': {u'AFFGEOID10': u'8600000US36426',
                 u'ALAND10': 1258584220,
                 u'AWATER10': 12694318,
                 u'GEOID10': u'36426',
                 u'ZCTA5CE10': u'36426'},
 u'type': u'Feature'}

In [26]:
"""
    Trimmed: 
"""
# same as the original
dict_temp = temp.to_dict(orient='records')
pprint(dict_temp[0])


{u'geometry': {u'coordinates': [[[-122.50808771796498,
                                  37.73519779053799,
                                  0.0],
                                 [-122.502558, 37.735557, 0.0],
                                 [-122.496449, 37.733877, 0.0],
                                 [-122.493784, 37.733989, 0.0],
                                 [-122.491294, 37.734096, 0.0],
                                 [-122.49146, 37.737283, 0.0],
                                 [-122.490364, 37.73793, 0.0],
                                 [-122.488518, 37.737002, 0.0],
                                 [-122.485767, 37.736757, 0.0],
                                 [-122.483971, 37.737387, 0.0],
                                 [-122.481727, 37.737172, 0.0],
                                 [-122.47539, 37.73745, 0.0],
                                 [-122.475221, 37.734869, 0.0],
                                 [-122.471569, 37.734707, 0.0],
                                 [-122.47244, 37.721603, 0.0],
                                 [-122.464294, 37.722114, 0.0],
                                 [-122.462271, 37.723021, 0.0],
                                 [-122.462683, 37.721692, 0.0],
                                 [-122.462556, 37.711254, 0.0],
                                 [-122.460858, 37.710586, 0.0],
                                 [-122.465994, 37.710154, 0.0],
                                 [-122.468939, 37.708232, 0.0],
                                 [-122.469236, 37.708232, 0.0],
                                 [-122.471485, 37.708331, 0.0],
                                 [-122.498302, 37.708132, 0.0],
                                 [-122.498161, 37.710091, 0.0],
                                 [-122.498442, 37.714886, 0.0],
                                 [-122.499906, 37.717286, 0.0],
                                 [-122.500403, 37.720144, 0.0],
                                 [-122.502374, 37.722898, 0.0],
                                 [-122.503128, 37.725218, 0.0],
                                 [-122.50532, 37.726322, 0.0],
                                 [-122.50705748535499,
                                  37.72783608474219,
                                  0.0],
                                 [-122.50808771796498,
                                  37.73519779053799,
                                  0.0]]],
               u'type': u'Polygon'},
 u'properties': {u'AFFGEOID10': u'8600000US94132',
                 u'ALAND10': 8054467,
                 u'AWATER10': 1299131,
                 u'GEOID10': u'94132',
                 u'ZCTA5CE10': u'94132'},
 u'type': u'Feature'}

In [27]:
# replace the original
data['features'] = dict_temp

In [28]:
# save as a new file
import json
with open('zip_cities.json', 'w') as f:
    json.dump(data, f)

In [29]:
# test using folium
m = folium.Map(location=[37.769959, -122.448679], zoom_start=9)
geo_path = 'zip_cities.json'
m.geo_json(geo_path= geo_path)
m.create_map('zip_cities.html')

In [31]:
# save the zip code file
import pickle
with open('zipcode_final.txt', 'wb') as f:
    pickle.dump(zipcodes_filtered, f)

In [ ]:
# # load the file 
# with open('zipcode_final.txt', 'rb') as f:
#     zip_list = pickle.load(f)

In [ ]: