/statewise_sidewalks.py Secret
Last active
March 6, 2018 12:05
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Import statements to get required modules | |
import pandas as pd | |
import numpy as np | |
import re | |
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 | |
#Loading the dataset into a dataframe | |
df=pd.read_csv('./Housing_Dataset.csv',encoding ='latin1') | |
dic={'Latitude':df['lat'],'Longitude':df['long'],'SideWalk':df['sidewalk_ok']} | |
tf=pd.DataFrame(dic) | |
tf=tf.dropna(axis=0, how='any') | |
tf = tf.reset_index(drop=True) | |
# Fetching the zipcodes based on Latitudes and Longitudes and mapping it to corresponding State name | |
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 != '#'] | |
mymap = {'yes':1, 'no':0,'Not sure, mixed':0} | |
tf=tf.applymap(lambda s: mymap.get(s) if s in mymap else s) | |
tf = tf.reset_index(drop=True) | |
# Plotting the results | |
sns.set_context("poster") | |
sns.factorplot(x="State_Name", y="SideWalk", data=tf, kind="bar",size=4, aspect=4) | |
plt.xticks(rotation=-90) | |
plt.xlabel('State_Name') | |
plt.ylabel('Fraction of houses having Side Walks') | |
plt.title('US stateWise fractions of houses having sidewalks') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment