Computational Exercises: Solutions > Analyzing Residential Property by Zip Code (g13)

parcels.py

"""
parcels.py
Spring 2022 PJW

Analyze residential property in Onondaga County by zip code.
"""

import pandas as pd
import matplotlib.pyplot as plt

#
#  Read the parcel data being careful not to clobber the GEOID
#

use_type = {'ZCTA5CE10':str}
raw = pd.read_csv('onondaga-tax-parcels.zip',dtype=use_type)
print( len(raw) )

#
#  Create a couple of variables for convenience
#

raw['zipcode'] = raw['ZCTA5CE10']
raw['value_k'] = raw['TOTAL_AV']/1000

#%%

#
#  Drop records that are missing any of the key variables below
#

key_vars = ['zipcode','value_k','PROP_CLASS']
usable = raw.dropna(subset=key_vars)
print( len(usable) )

#
#  Filter down to residential property
#

is_res = usable['PROP_CLASS'].between(200,299)
res = usable[ is_res ]

#%%

#
#  Compute some overall statistics
#

tot_n = len(res)
mean_k = res['value_k'].mean()
tot_bil = res['value_k'].sum()/1e6

print( 'Residential parcels:', tot_n )
print( 'Mean value, thousand $:', round(mean_k,2) )
print( 'Total value, billion $:', round(tot_bil,2) )

#%%

#
#  Group by zip code, build a dataframe with the medians of 
#  selected variables, and print it out
#

by_zip = res.groupby('zipcode')

attr = ['SQFT_LIV', 'NBR_BEDRM', 'YR_BLT', 'CALC_ACRES']
summary = by_zip[attr].median()
summary = summary.round(2)
summary = summary.sort_values('YR_BLT')

print( summary )

#%%

#
#  Now look at values by zip code in more detail
#

value = by_zip['value_k'].describe()
value = value.round(2)
value = value.sort_values('50%')

print( value.sort_values('50%') )

#%%

# 
#  Make a nice summary dataframe and save it as a csv file
#

for c in value.columns:
    summary[c] = value[c]

summary = summary.sort_index()
summary.to_csv('parcels.csv')

#%%

#
#  Now draw a plot showing three characteristics of housing by 
#  zip code: year built, square feet of living area, and median 
#  value; dots will be scaled by value
#

(fig1, ax1) = plt.subplots()
summary.plot.scatter('YR_BLT','SQFT_LIV',s='50%',ax=ax1)
ax1.set_title('Characteristics of Zip Codes')
fig1.tight_layout()
fig1.savefig('parcels.png',dpi=300)
Site Index | Zoom | Admin
URL: https://wilcoxen.maxwell.insightworks.com/pages/6232.html
Peter J Wilcoxen, The Maxwell School, Syracuse University
Revised 02/26/2022