Skip to content

Instantly share code, notes, and snippets.

@abhimanyu-bitsgoa
Last active March 6, 2018 12:01
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/eddb37271dca669ab887f95008c419f0 to your computer and use it in GitHub Desktop.
Save abhimanyu-bitsgoa/eddb37271dca669ab887f95008c419f0 to your computer and use it in GitHub Desktop.
#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'],'House_Type':df['house_types']}
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 = {'private_house':1, 'apartment_blocks':0,'other':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="House_Type", data=tf, kind="bar",size=4, aspect=4)
plt.xticks(rotation=-90)
plt.xlabel('State_Name')
plt.ylabel('Fraction of houses which are Private')
plt.title('US stateWise fractions of Private house')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment