Skip to content

Instantly share code, notes, and snippets.

@abhimanyu-bitsgoa
Last active March 6, 2018 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhimanyu-bitsgoa/6e6ab832b3f6bbff07b1420ab76abb2f to your computer and use it in GitHub Desktop.
Save abhimanyu-bitsgoa/6e6ab832b3f6bbff07b1420ab76abb2f to your computer and use it in GitHub Desktop.
#Import statements to get required modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import pie, axis, show
%matplotlib inline
import seaborn as sns
import math
from uszipcode import ZipcodeSearchEngine
import us
import json
import gmplot
#Loading the dataset into a dataframe
df=pd.read_csv('./Housing_Dataset.csv',encoding ='latin1')
dic={'Latitude':df['lat'],'Longitude':df['long'],'Residence':df['residential_yes']}
tf=pd.DataFrame(dic)
tf=tf.dropna(axis=0, how='any')
tf = tf.reset_index(drop=True)
search = ZipcodeSearchEngine()
StateNames=[]
for lat,lon in zip(tf['Latitude'],tf['Longitude']):
res = search.by_coordinate(lat, lon, radius=30, returns=1)
jres=json.loads(str(res))
if (len(jres)>0):
stateCode=jres[0]['State']
StateNames.append(us.states.lookup(stateCode))
else:
StateNames.append('#')
tf['State_Name']=StateNames
tf = tf[tf.State_Name != '#']
tf = tf[tf.Residence != 'not_avail']
mymap = {'yes':1, 'no':0}
tf=tf.applymap(lambda s: mymap.get(s) if s in mymap else s)
tfr = tf[tf.Residence == 1]
tfnr = tf[tf.Residence == 0]
tf = tf.reset_index(drop=True)
tfr = tfr.reset_index(drop=True)
tfnr = tfnr.reset_index(drop=True)
#Plotting the results on a map
lats1=tfr['Latitude']
lons1=tfr['Longitude']
lats2=tfnr['Latitude']
lons2=tfnr['Longitude']
gmap = gmplot.GoogleMapPlotter(lats1[0], lons1[0], 16)
gmap.scatter(lats1, lons1, '#00FA9A', marker=True) #Residential = Green
gmap.scatter(lats2, lons2, '#8B0000', marker=True) #Non residential = Red
gmap.draw("house_plot.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment