Wrangling Data

Author: Avikal Somvanshi


In [4]:
from __future__ import print_function, division
import pandas as pd
import pylab as pl
import os
import csv
from pandas.tools.plotting import scatter_matrix
import sys
reload(sys)
import zipfile
sys.setdefaultencoding('utf-8')
%pylab inline

In [6]:
#Unzippping downloaded median income data of 1999 in a dataframe
zipfile.ZipFile(os.path.join("data/median_hincome_2000.zip")).extractall(r"data/median_hincome_2000")

In [11]:
#Reading and saving downloaded median income data of 1999 in a dataframe
data_2000 = pd.read_csv('data/median_hincome_2000/DEC_00_SF3_P053_with_ann.csv')
data_2000


Out[11]:
GEO.id GEO.id2 GEO.display-label VD01
0 Id Id2 Geography Median household income in 1999
1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 69821
2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 60139
3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 57500
4 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 36750
5 1500000US360470002002 360470002002 Block Group 2, Census Tract 2, Kings County, N... 24706
6 1500000US360470003011 360470003011 Block Group 1, Census Tract 3.01, Kings County... 77309
7 1500000US360470003012 360470003012 Block Group 2, Census Tract 3.01, Kings County... 87010
8 1500000US360470003013 360470003013 Block Group 3, Census Tract 3.01, Kings County... 72406
9 1500000US360470003014 360470003014 Block Group 4, Census Tract 3.01, Kings County... 93674
10 1500000US360470003015 360470003015 Block Group 5, Census Tract 3.01, Kings County... 66875
11 1500000US360470003021 360470003021 Block Group 1, Census Tract 3.02, Kings County... 112414
12 1500000US360470005001 360470005001 Block Group 1, Census Tract 5, Kings County, N... 60727
13 1500000US360470005002 360470005002 Block Group 2, Census Tract 5, Kings County, N... 67308
14 1500000US360470005003 360470005003 Block Group 3, Census Tract 5, Kings County, N... 87895
15 1500000US360470005004 360470005004 Block Group 4, Census Tract 5, Kings County, N... 44306
16 1500000US360470005005 360470005005 Block Group 5, Census Tract 5, Kings County, N... 65357
17 1500000US360470007001 360470007001 Block Group 1, Census Tract 7, Kings County, N... 65893
18 1500000US360470007002 360470007002 Block Group 2, Census Tract 7, Kings County, N... 57730
19 1500000US360470007003 360470007003 Block Group 3, Census Tract 7, Kings County, N... 91483
20 1500000US360470009001 360470009001 Block Group 1, Census Tract 9, Kings County, N... 80685
21 1500000US360470009002 360470009002 Block Group 2, Census Tract 9, Kings County, N... 69375
22 1500000US360470011001 360470011001 Block Group 1, Census Tract 11, Kings County, ... 29732
23 1500000US360470013001 360470013001 Block Group 1, Census Tract 13, Kings County, ... 59245
24 1500000US360470013002 360470013002 Block Group 2, Census Tract 13, Kings County, ... 45278
25 1500000US360470018001 360470018001 Block Group 1, Census Tract 18, Kings County, ... 85000
26 1500000US360470020001 360470020001 Block Group 1, Census Tract 20, Kings County, ... 27500
27 1500000US360470020002 360470020002 Block Group 2, Census Tract 20, Kings County, ... 16607
28 1500000US360470020003 360470020003 Block Group 3, Census Tract 20, Kings County, ... 33750
29 1500000US360470021001 360470021001 Block Group 1, Census Tract 21, Kings County, ... 47308
... ... ... ... ...
2032 1500000US360471184001 360471184001 Block Group 1, Census Tract 1184, Kings County... 24063
2033 1500000US360471184002 360471184002 Block Group 2, Census Tract 1184, Kings County... 33350
2034 1500000US360471184003 360471184003 Block Group 3, Census Tract 1184, Kings County... 35888
2035 1500000US360471186001 360471186001 Block Group 1, Census Tract 1186, Kings County... 31164
2036 1500000US360471186002 360471186002 Block Group 2, Census Tract 1186, Kings County... 37083
2037 1500000US360471188001 360471188001 Block Group 1, Census Tract 1188, Kings County... 22188
2038 1500000US360471188002 360471188002 Block Group 2, Census Tract 1188, Kings County... 25000
2039 1500000US360471188003 360471188003 Block Group 3, Census Tract 1188, Kings County... 16757
2040 1500000US360471190001 360471190001 Block Group 1, Census Tract 1190, Kings County... 25093
2041 1500000US360471192001 360471192001 Block Group 1, Census Tract 1192, Kings County... 24000
2042 1500000US360471192002 360471192002 Block Group 2, Census Tract 1192, Kings County... 28750
2043 1500000US360471192003 360471192003 Block Group 3, Census Tract 1192, Kings County... 35500
2044 1500000US360471194001 360471194001 Block Group 1, Census Tract 1194, Kings County... 21705
2045 1500000US360471194002 360471194002 Block Group 2, Census Tract 1194, Kings County... 34271
2046 1500000US360471194003 360471194003 Block Group 3, Census Tract 1194, Kings County... 30625
2047 1500000US360471196001 360471196001 Block Group 1, Census Tract 1196, Kings County... 28452
2048 1500000US360471196002 360471196002 Block Group 2, Census Tract 1196, Kings County... 40278
2049 1500000US360471196003 360471196003 Block Group 3, Census Tract 1196, Kings County... 24038
2050 1500000US360471196004 360471196004 Block Group 4, Census Tract 1196, Kings County... 30089
2051 1500000US360471200001 360471200001 Block Group 1, Census Tract 1200, Kings County... 19706
2052 1500000US360471200002 360471200002 Block Group 2, Census Tract 1200, Kings County... 38214
2053 1500000US360471202001 360471202001 Block Group 1, Census Tract 1202, Kings County... 39886
2054 1500000US360471202002 360471202002 Block Group 2, Census Tract 1202, Kings County... 17644
2055 1500000US360471208001 360471208001 Block Group 1, Census Tract 1208, Kings County... 70556
2056 1500000US360471208002 360471208002 Block Group 2, Census Tract 1208, Kings County... 30723
2057 1500000US360471208003 360471208003 Block Group 3, Census Tract 1208, Kings County... 25491
2058 1500000US360471210001 360471210001 Block Group 1, Census Tract 1210, Kings County... 9919
2059 1500000US360471214001 360471214001 Block Group 1, Census Tract 1214, Kings County... 8927
2060 1500000US360471220001 360471220001 Block Group 1, Census Tract 1220, Kings County... 22255
2061 1500000US360471220002 360471220002 Block Group 2, Census Tract 1220, Kings County... 44722

2062 rows × 4 columns


In [8]:
#Unzipping downloaded median income data of 2013 in a dataframe
zipfile.ZipFile(os.path.join("data/median_hincome_2013.zip")).extractall(r"data/median_hincome_2013")

In [12]:
#Reading and saving downloaded median income data of 2013 in a dataframe
data_2013 = pd.read_csv('data/median_hincome_2013/ACS_13_5YR_B19013_with_ann.csv')
data_2013


Out[12]:
GEO.id GEO.id2 GEO.display-label HD01_VD01 HD02_VD01
0 Id Id2 Geography Estimate; Median household income in the past ... Margin of Error; Median household income in th...
1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 99896 62051
2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 42396 205413
3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 95526 10879
4 1500000US360470001004 360470001004 Block Group 4, Census Tract 1, Kings County, N... 68393 12890
5 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 33906 5772
6 1500000US360470003011 360470003011 Block Group 1, Census Tract 3.01, Kings County... 120375 18781
7 1500000US360470003012 360470003012 Block Group 2, Census Tract 3.01, Kings County... 77560 96417
8 1500000US360470003013 360470003013 Block Group 3, Census Tract 3.01, Kings County... 174100 52098
9 1500000US360470003014 360470003014 Block Group 4, Census Tract 3.01, Kings County... 82361 59456
10 1500000US360470003015 360470003015 Block Group 5, Census Tract 3.01, Kings County... 99500 103553
11 1500000US360470005011 360470005011 Block Group 1, Census Tract 5.01, Kings County... 90563 30675
12 1500000US360470005012 360470005012 Block Group 2, Census Tract 5.01, Kings County... 98068 50615
13 1500000US360470005013 360470005013 Block Group 3, Census Tract 5.01, Kings County... 130298 47904
14 1500000US360470005021 360470005021 Block Group 1, Census Tract 5.02, Kings County... 91208 9689
15 1500000US360470005022 360470005022 Block Group 2, Census Tract 5.02, Kings County... 113250 53627
16 1500000US360470007001 360470007001 Block Group 1, Census Tract 7, Kings County, N... 131023 39266
17 1500000US360470007002 360470007002 Block Group 2, Census Tract 7, Kings County, N... 135192 33965
18 1500000US360470007003 360470007003 Block Group 3, Census Tract 7, Kings County, N... 142292 34040
19 1500000US360470009001 360470009001 Block Group 1, Census Tract 9, Kings County, N... 106094 20132
20 1500000US360470009002 360470009002 Block Group 2, Census Tract 9, Kings County, N... 166667 57801
21 1500000US360470011001 360470011001 Block Group 1, Census Tract 11, Kings County, ... 120208 37036
22 1500000US360470013001 360470013001 Block Group 1, Census Tract 13, Kings County, ... 108170 10300
23 1500000US360470013002 360470013002 Block Group 2, Census Tract 13, Kings County, ... - **
24 1500000US360470015001 360470015001 Block Group 1, Census Tract 15, Kings County, ... 111389 18272
25 1500000US360470015002 360470015002 Block Group 2, Census Tract 15, Kings County, ... 19737 3954
26 1500000US360470015003 360470015003 Block Group 3, Census Tract 15, Kings County, ... 92566 14869
27 1500000US360470018000 360470018000 Block Group 0, Census Tract 18, Kings County, ... - **
28 1500000US360470018001 360470018001 Block Group 1, Census Tract 18, Kings County, ... - **
29 1500000US360470020001 360470020001 Block Group 1, Census Tract 20, Kings County, ... 34857 6364
... ... ... ... ... ...
2056 1500000US360471196003 360471196003 Block Group 3, Census Tract 1196, Kings County... 40909 14672
2057 1500000US360471196004 360471196004 Block Group 4, Census Tract 1196, Kings County... 36824 6405
2058 1500000US360471198001 360471198001 Block Group 1, Census Tract 1198, Kings County... 42861 11033
2059 1500000US360471198002 360471198002 Block Group 2, Census Tract 1198, Kings County... 17083 31684
2060 1500000US360471198003 360471198003 Block Group 3, Census Tract 1198, Kings County... 23413 10897
2061 1500000US360471200001 360471200001 Block Group 1, Census Tract 1200, Kings County... 26518 12626
2062 1500000US360471200002 360471200002 Block Group 2, Census Tract 1200, Kings County... 36406 11876
2063 1500000US360471202001 360471202001 Block Group 1, Census Tract 1202, Kings County... 43542 18344
2064 1500000US360471202002 360471202002 Block Group 2, Census Tract 1202, Kings County... 23125 10708
2065 1500000US360471208001 360471208001 Block Group 1, Census Tract 1208, Kings County... 42542 18989
2066 1500000US360471208002 360471208002 Block Group 2, Census Tract 1208, Kings County... 43261 12182
2067 1500000US360471208003 360471208003 Block Group 3, Census Tract 1208, Kings County... 26037 14336
2068 1500000US360471208004 360471208004 Block Group 4, Census Tract 1208, Kings County... 31085 8737
2069 1500000US360471208005 360471208005 Block Group 5, Census Tract 1208, Kings County... 37188 26229
2070 1500000US360471210001 360471210001 Block Group 1, Census Tract 1210, Kings County... 15625 10182
2071 1500000US360471210002 360471210002 Block Group 2, Census Tract 1210, Kings County... 16500 6864
2072 1500000US360471214001 360471214001 Block Group 1, Census Tract 1214, Kings County... 17966 2924
2073 1500000US360471214002 360471214002 Block Group 2, Census Tract 1214, Kings County... 17264 4292
2074 1500000US360471220001 360471220001 Block Group 1, Census Tract 1220, Kings County... 32177 12326
2075 1500000US360471220002 360471220002 Block Group 2, Census Tract 1220, Kings County... 45390 9149
2076 1500000US360471237001 360471237001 Block Group 1, Census Tract 1237, Kings County... 28640 19413
2077 1500000US360471237002 360471237002 Block Group 2, Census Tract 1237, Kings County... 32500 15037
2078 1500000US360471237003 360471237003 Block Group 3, Census Tract 1237, Kings County... 32386 14283
2079 1500000US360471502001 360471502001 Block Group 1, Census Tract 1502, Kings County... 96167 31027
2080 1500000US360471502002 360471502002 Block Group 2, Census Tract 1502, Kings County... 102273 11886
2081 1500000US360471502003 360471502003 Block Group 3, Census Tract 1502, Kings County... 86215 24226
2082 1500000US360471522001 360471522001 Block Group 1, Census Tract 1522, Kings County... 80694 14977
2083 1500000US360471522002 360471522002 Block Group 2, Census Tract 1522, Kings County... 92778 40277
2084 1500000US360471522003 360471522003 Block Group 3, Census Tract 1522, Kings County... 88205 18601
2085 1500000US360479901000 360479901000 Block Group 0, Census Tract 9901, Kings County... - **

2086 rows × 5 columns


In [15]:
#Merging all the dataframes to a mother dataframe
m_income = pd.merge(data_2000, data_2013, how='outer', on=['GEO.id', 'GEO.id2', 'GEO.display-label'])
m_income


Out[15]:
GEO.id GEO.id2 GEO.display-label VD01 HD01_VD01 HD02_VD01
0 Id Id2 Geography Median household income in 1999 Estimate; Median household income in the past ... Margin of Error; Median household income in th...
1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 69821 99896 62051
2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 60139 42396 205413
3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 57500 95526 10879
4 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 36750 33906 5772
5 1500000US360470002002 360470002002 Block Group 2, Census Tract 2, Kings County, N... 24706 NaN NaN
6 1500000US360470003011 360470003011 Block Group 1, Census Tract 3.01, Kings County... 77309 120375 18781
7 1500000US360470003012 360470003012 Block Group 2, Census Tract 3.01, Kings County... 87010 77560 96417
8 1500000US360470003013 360470003013 Block Group 3, Census Tract 3.01, Kings County... 72406 174100 52098
9 1500000US360470003014 360470003014 Block Group 4, Census Tract 3.01, Kings County... 93674 82361 59456
10 1500000US360470003015 360470003015 Block Group 5, Census Tract 3.01, Kings County... 66875 99500 103553
11 1500000US360470003021 360470003021 Block Group 1, Census Tract 3.02, Kings County... 112414 NaN NaN
12 1500000US360470005001 360470005001 Block Group 1, Census Tract 5, Kings County, N... 60727 NaN NaN
13 1500000US360470005002 360470005002 Block Group 2, Census Tract 5, Kings County, N... 67308 NaN NaN
14 1500000US360470005003 360470005003 Block Group 3, Census Tract 5, Kings County, N... 87895 NaN NaN
15 1500000US360470005004 360470005004 Block Group 4, Census Tract 5, Kings County, N... 44306 NaN NaN
16 1500000US360470005005 360470005005 Block Group 5, Census Tract 5, Kings County, N... 65357 NaN NaN
17 1500000US360470007001 360470007001 Block Group 1, Census Tract 7, Kings County, N... 65893 131023 39266
18 1500000US360470007002 360470007002 Block Group 2, Census Tract 7, Kings County, N... 57730 135192 33965
19 1500000US360470007003 360470007003 Block Group 3, Census Tract 7, Kings County, N... 91483 142292 34040
20 1500000US360470009001 360470009001 Block Group 1, Census Tract 9, Kings County, N... 80685 106094 20132
21 1500000US360470009002 360470009002 Block Group 2, Census Tract 9, Kings County, N... 69375 166667 57801
22 1500000US360470011001 360470011001 Block Group 1, Census Tract 11, Kings County, ... 29732 120208 37036
23 1500000US360470013001 360470013001 Block Group 1, Census Tract 13, Kings County, ... 59245 108170 10300
24 1500000US360470013002 360470013002 Block Group 2, Census Tract 13, Kings County, ... 45278 - **
25 1500000US360470018001 360470018001 Block Group 1, Census Tract 18, Kings County, ... 85000 - **
26 1500000US360470020001 360470020001 Block Group 1, Census Tract 20, Kings County, ... 27500 34857 6364
27 1500000US360470020002 360470020002 Block Group 2, Census Tract 20, Kings County, ... 16607 75476 4501
28 1500000US360470020003 360470020003 Block Group 3, Census Tract 20, Kings County, ... 33750 NaN NaN
29 1500000US360470021001 360470021001 Block Group 1, Census Tract 21, Kings County, ... 47308 250,000+ ***
... ... ... ... ... ... ...
2275 1500000US360471104001 360471104001 Block Group 1, Census Tract 1104, Kings County... NaN 52375 29981
2276 1500000US360471104002 360471104002 Block Group 2, Census Tract 1104, Kings County... NaN 27083 8005
2277 1500000US360471104003 360471104003 Block Group 3, Census Tract 1104, Kings County... NaN 44671 31607
2278 1500000US360471104004 360471104004 Block Group 4, Census Tract 1104, Kings County... NaN 55217 42111
2279 1500000US360471106002 360471106002 Block Group 2, Census Tract 1106, Kings County... NaN 37336 6580
2280 1500000US360471110002 360471110002 Block Group 2, Census Tract 1110, Kings County... NaN 30789 7836
2281 1500000US360471116001 360471116001 Block Group 1, Census Tract 1116, Kings County... NaN 26616 16502
2282 1500000US360471116002 360471116002 Block Group 2, Census Tract 1116, Kings County... NaN 37821 20871
2283 1500000US360471144001 360471144001 Block Group 1, Census Tract 1144, Kings County... NaN 42946 48049
2284 1500000US360471144002 360471144002 Block Group 2, Census Tract 1144, Kings County... NaN 31667 21607
2285 1500000US360471144003 360471144003 Block Group 3, Census Tract 1144, Kings County... NaN 23750 21843
2286 1500000US360471144004 360471144004 Block Group 4, Census Tract 1144, Kings County... NaN 24821 4382
2287 1500000US360471190002 360471190002 Block Group 2, Census Tract 1190, Kings County... NaN 27778 12643
2288 1500000US360471198001 360471198001 Block Group 1, Census Tract 1198, Kings County... NaN 42861 11033
2289 1500000US360471198002 360471198002 Block Group 2, Census Tract 1198, Kings County... NaN 17083 31684
2290 1500000US360471198003 360471198003 Block Group 3, Census Tract 1198, Kings County... NaN 23413 10897
2291 1500000US360471208004 360471208004 Block Group 4, Census Tract 1208, Kings County... NaN 31085 8737
2292 1500000US360471208005 360471208005 Block Group 5, Census Tract 1208, Kings County... NaN 37188 26229
2293 1500000US360471210002 360471210002 Block Group 2, Census Tract 1210, Kings County... NaN 16500 6864
2294 1500000US360471214002 360471214002 Block Group 2, Census Tract 1214, Kings County... NaN 17264 4292
2295 1500000US360471237001 360471237001 Block Group 1, Census Tract 1237, Kings County... NaN 28640 19413
2296 1500000US360471237002 360471237002 Block Group 2, Census Tract 1237, Kings County... NaN 32500 15037
2297 1500000US360471237003 360471237003 Block Group 3, Census Tract 1237, Kings County... NaN 32386 14283
2298 1500000US360471502001 360471502001 Block Group 1, Census Tract 1502, Kings County... NaN 96167 31027
2299 1500000US360471502002 360471502002 Block Group 2, Census Tract 1502, Kings County... NaN 102273 11886
2300 1500000US360471502003 360471502003 Block Group 3, Census Tract 1502, Kings County... NaN 86215 24226
2301 1500000US360471522001 360471522001 Block Group 1, Census Tract 1522, Kings County... NaN 80694 14977
2302 1500000US360471522002 360471522002 Block Group 2, Census Tract 1522, Kings County... NaN 92778 40277
2303 1500000US360471522003 360471522003 Block Group 3, Census Tract 1522, Kings County... NaN 88205 18601
2304 1500000US360479901000 360479901000 Block Group 0, Census Tract 9901, Kings County... NaN - **

2305 rows × 6 columns


In [17]:
#Saving the dataset as csv
m_income.to_csv('m_income.csv')

In [18]:
#Checking the CSV
data = pd.read_csv('m_income.csv')
data.head()


Out[18]:
Unnamed: 0 GEO.id GEO.id2 GEO.display-label VD01 HD01_VD01 HD02_VD01
0 0 Id Id2 Geography Median household income in 1999 Estimate; Median household income in the past ... Margin of Error; Median household income in th...
1 1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 69821 99896 62051
2 2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 60139 42396 205413
3 3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 57500 95526 10879
4 4 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 36750 33906 5772

Good to go!

Rent Data


In [19]:
#Unzippping downloaded rent data of 1999 in a dataframe
zipfile.ZipFile(os.path.join("data/median_rent_2000.zip")).extractall(r"data/median_rent_2000")

In [20]:
#Reading and saving downloaded median income data of 1999 in a dataframe
rdata_2000 = pd.read_csv('data/median_rent_2000/DEC_00_SF3_H063_with_ann.csv')
rdata_2000


Out[20]:
GEO.id GEO.id2 GEO.display-label VD01
0 Id Id2 Geography Median gross rent
1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 750
2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 671
3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 904
4 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 835
5 1500000US360470002002 360470002002 Block Group 2, Census Tract 2, Kings County, N... 690
6 1500000US360470003011 360470003011 Block Group 1, Census Tract 3.01, Kings County... 1000
7 1500000US360470003012 360470003012 Block Group 2, Census Tract 3.01, Kings County... 1046
8 1500000US360470003013 360470003013 Block Group 3, Census Tract 3.01, Kings County... 1075
9 1500000US360470003014 360470003014 Block Group 4, Census Tract 3.01, Kings County... 863
10 1500000US360470003015 360470003015 Block Group 5, Census Tract 3.01, Kings County... 843
11 1500000US360470003021 360470003021 Block Group 1, Census Tract 3.02, Kings County... 1150
12 1500000US360470005001 360470005001 Block Group 1, Census Tract 5, Kings County, N... 988
13 1500000US360470005002 360470005002 Block Group 2, Census Tract 5, Kings County, N... 889
14 1500000US360470005003 360470005003 Block Group 3, Census Tract 5, Kings County, N... 1123
15 1500000US360470005004 360470005004 Block Group 4, Census Tract 5, Kings County, N... 498
16 1500000US360470005005 360470005005 Block Group 5, Census Tract 5, Kings County, N... 943
17 1500000US360470007001 360470007001 Block Group 1, Census Tract 7, Kings County, N... 1139
18 1500000US360470007002 360470007002 Block Group 2, Census Tract 7, Kings County, N... 1077
19 1500000US360470007003 360470007003 Block Group 3, Census Tract 7, Kings County, N... 954
20 1500000US360470009001 360470009001 Block Group 1, Census Tract 9, Kings County, N... 851
21 1500000US360470009002 360470009002 Block Group 2, Census Tract 9, Kings County, N... 1164
22 1500000US360470011001 360470011001 Block Group 1, Census Tract 11, Kings County, ... 701
23 1500000US360470013001 360470013001 Block Group 1, Census Tract 13, Kings County, ... 1107
24 1500000US360470013002 360470013002 Block Group 2, Census Tract 13, Kings County, ... 825
25 1500000US360470018001 360470018001 Block Group 1, Census Tract 18, Kings County, ... 950
26 1500000US360470020001 360470020001 Block Group 1, Census Tract 20, Kings County, ... 758
27 1500000US360470020002 360470020002 Block Group 2, Census Tract 20, Kings County, ... 683
28 1500000US360470020003 360470020003 Block Group 3, Census Tract 20, Kings County, ... 644
29 1500000US360470021001 360470021001 Block Group 1, Census Tract 21, Kings County, ... 950
... ... ... ... ...
2032 1500000US360471184001 360471184001 Block Group 1, Census Tract 1184, Kings County... 784
2033 1500000US360471184002 360471184002 Block Group 2, Census Tract 1184, Kings County... 719
2034 1500000US360471184003 360471184003 Block Group 3, Census Tract 1184, Kings County... 723
2035 1500000US360471186001 360471186001 Block Group 1, Census Tract 1186, Kings County... 785
2036 1500000US360471186002 360471186002 Block Group 2, Census Tract 1186, Kings County... 588
2037 1500000US360471188001 360471188001 Block Group 1, Census Tract 1188, Kings County... 782
2038 1500000US360471188002 360471188002 Block Group 2, Census Tract 1188, Kings County... 787
2039 1500000US360471188003 360471188003 Block Group 3, Census Tract 1188, Kings County... 659
2040 1500000US360471190001 360471190001 Block Group 1, Census Tract 1190, Kings County... 771
2041 1500000US360471192001 360471192001 Block Group 1, Census Tract 1192, Kings County... 627
2042 1500000US360471192002 360471192002 Block Group 2, Census Tract 1192, Kings County... 605
2043 1500000US360471192003 360471192003 Block Group 3, Census Tract 1192, Kings County... 618
2044 1500000US360471194001 360471194001 Block Group 1, Census Tract 1194, Kings County... 741
2045 1500000US360471194002 360471194002 Block Group 2, Census Tract 1194, Kings County... 635
2046 1500000US360471194003 360471194003 Block Group 3, Census Tract 1194, Kings County... 677
2047 1500000US360471196001 360471196001 Block Group 1, Census Tract 1196, Kings County... 709
2048 1500000US360471196002 360471196002 Block Group 2, Census Tract 1196, Kings County... 633
2049 1500000US360471196003 360471196003 Block Group 3, Census Tract 1196, Kings County... 624
2050 1500000US360471196004 360471196004 Block Group 4, Census Tract 1196, Kings County... 724
2051 1500000US360471200001 360471200001 Block Group 1, Census Tract 1200, Kings County... 711
2052 1500000US360471200002 360471200002 Block Group 2, Census Tract 1200, Kings County... 702
2053 1500000US360471202001 360471202001 Block Group 1, Census Tract 1202, Kings County... 788
2054 1500000US360471202002 360471202002 Block Group 2, Census Tract 1202, Kings County... 914
2055 1500000US360471208001 360471208001 Block Group 1, Census Tract 1208, Kings County... 805
2056 1500000US360471208002 360471208002 Block Group 2, Census Tract 1208, Kings County... 573
2057 1500000US360471208003 360471208003 Block Group 3, Census Tract 1208, Kings County... 731
2058 1500000US360471210001 360471210001 Block Group 1, Census Tract 1210, Kings County... 208
2059 1500000US360471214001 360471214001 Block Group 1, Census Tract 1214, Kings County... 221
2060 1500000US360471220001 360471220001 Block Group 1, Census Tract 1220, Kings County... 611
2061 1500000US360471220002 360471220002 Block Group 2, Census Tract 1220, Kings County... 913

2062 rows × 4 columns


In [21]:
#Unzipping downloaded median income data of 2013 in a dataframe
zipfile.ZipFile(os.path.join("data/median_rent_2013.zip")).extractall(r"data/median_rent_2013")

In [22]:
#Reading and saving downloaded median income data of 2013 in a dataframe
rdata_2013 = pd.read_csv('data/median_rent_2013/ACS_13_5YR_B25064_with_ann.csv')
rdata_2013


Out[22]:
GEO.id GEO.id2 GEO.display-label HD01_VD01 HD02_VD01
0 Id Id2 Geography Estimate; Median gross rent Margin of Error; Median gross rent
1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 1867 1053
2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 1450 315
3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 1558 211
4 1500000US360470001004 360470001004 Block Group 4, Census Tract 1, Kings County, N... 944 230
5 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 1228 122
6 1500000US360470003011 360470003011 Block Group 1, Census Tract 3.01, Kings County... 2,000+ ***
7 1500000US360470003012 360470003012 Block Group 2, Census Tract 3.01, Kings County... 1535 366
8 1500000US360470003013 360470003013 Block Group 3, Census Tract 3.01, Kings County... 2,000+ ***
9 1500000US360470003014 360470003014 Block Group 4, Census Tract 3.01, Kings County... 1795 165
10 1500000US360470003015 360470003015 Block Group 5, Census Tract 3.01, Kings County... - **
11 1500000US360470005011 360470005011 Block Group 1, Census Tract 5.01, Kings County... 1708 393
12 1500000US360470005012 360470005012 Block Group 2, Census Tract 5.01, Kings County... 2,000+ ***
13 1500000US360470005013 360470005013 Block Group 3, Census Tract 5.01, Kings County... 2,000+ ***
14 1500000US360470005021 360470005021 Block Group 1, Census Tract 5.02, Kings County... 2,000+ ***
15 1500000US360470005022 360470005022 Block Group 2, Census Tract 5.02, Kings County... 1748 289
16 1500000US360470007001 360470007001 Block Group 1, Census Tract 7, Kings County, N... 1733 128
17 1500000US360470007002 360470007002 Block Group 2, Census Tract 7, Kings County, N... 1944 235
18 1500000US360470007003 360470007003 Block Group 3, Census Tract 7, Kings County, N... 2,000+ ***
19 1500000US360470009001 360470009001 Block Group 1, Census Tract 9, Kings County, N... 2,000+ ***
20 1500000US360470009002 360470009002 Block Group 2, Census Tract 9, Kings County, N... 2,000+ ***
21 1500000US360470011001 360470011001 Block Group 1, Census Tract 11, Kings County, ... 2,000+ ***
22 1500000US360470013001 360470013001 Block Group 1, Census Tract 13, Kings County, ... 1765 438
23 1500000US360470013002 360470013002 Block Group 2, Census Tract 13, Kings County, ... - **
24 1500000US360470015001 360470015001 Block Group 1, Census Tract 15, Kings County, ... 2,000+ ***
25 1500000US360470015002 360470015002 Block Group 2, Census Tract 15, Kings County, ... 427 86
26 1500000US360470015003 360470015003 Block Group 3, Census Tract 15, Kings County, ... 2,000+ ***
27 1500000US360470018000 360470018000 Block Group 0, Census Tract 18, Kings County, ... - **
28 1500000US360470018001 360470018001 Block Group 1, Census Tract 18, Kings County, ... - **
29 1500000US360470020001 360470020001 Block Group 1, Census Tract 20, Kings County, ... 1321 89
... ... ... ... ... ...
2056 1500000US360471196003 360471196003 Block Group 3, Census Tract 1196, Kings County... 1168 84
2057 1500000US360471196004 360471196004 Block Group 4, Census Tract 1196, Kings County... 1196 118
2058 1500000US360471198001 360471198001 Block Group 1, Census Tract 1198, Kings County... 1223 375
2059 1500000US360471198002 360471198002 Block Group 2, Census Tract 1198, Kings County... 1034 154
2060 1500000US360471198003 360471198003 Block Group 3, Census Tract 1198, Kings County... 1299 415
2061 1500000US360471200001 360471200001 Block Group 1, Census Tract 1200, Kings County... 1377 55
2062 1500000US360471200002 360471200002 Block Group 2, Census Tract 1200, Kings County... 1222 133
2063 1500000US360471202001 360471202001 Block Group 1, Census Tract 1202, Kings County... 1503 337
2064 1500000US360471202002 360471202002 Block Group 2, Census Tract 1202, Kings County... 1347 311
2065 1500000US360471208001 360471208001 Block Group 1, Census Tract 1208, Kings County... 1099 78
2066 1500000US360471208002 360471208002 Block Group 2, Census Tract 1208, Kings County... 1135 69
2067 1500000US360471208003 360471208003 Block Group 3, Census Tract 1208, Kings County... 1043 78
2068 1500000US360471208004 360471208004 Block Group 4, Census Tract 1208, Kings County... 1060 125
2069 1500000US360471208005 360471208005 Block Group 5, Census Tract 1208, Kings County... 1230 331
2070 1500000US360471210001 360471210001 Block Group 1, Census Tract 1210, Kings County... 441 96
2071 1500000US360471210002 360471210002 Block Group 2, Census Tract 1210, Kings County... 563 42
2072 1500000US360471214001 360471214001 Block Group 1, Census Tract 1214, Kings County... 505 56
2073 1500000US360471214002 360471214002 Block Group 2, Census Tract 1214, Kings County... 338 62
2074 1500000US360471220001 360471220001 Block Group 1, Census Tract 1220, Kings County... 1086 66
2075 1500000US360471220002 360471220002 Block Group 2, Census Tract 1220, Kings County... 1185 87
2076 1500000US360471237001 360471237001 Block Group 1, Census Tract 1237, Kings County... 1245 125
2077 1500000US360471237002 360471237002 Block Group 2, Census Tract 1237, Kings County... 1415 801
2078 1500000US360471237003 360471237003 Block Group 3, Census Tract 1237, Kings County... 1588 297
2079 1500000US360471502001 360471502001 Block Group 1, Census Tract 1502, Kings County... 1754 276
2080 1500000US360471502002 360471502002 Block Group 2, Census Tract 1502, Kings County... 2,000+ ***
2081 1500000US360471502003 360471502003 Block Group 3, Census Tract 1502, Kings County... 1204 513
2082 1500000US360471522001 360471522001 Block Group 1, Census Tract 1522, Kings County... 1540 198
2083 1500000US360471522002 360471522002 Block Group 2, Census Tract 1522, Kings County... 1353 192
2084 1500000US360471522003 360471522003 Block Group 3, Census Tract 1522, Kings County... 2,000+ ***
2085 1500000US360479901000 360479901000 Block Group 0, Census Tract 9901, Kings County... - **

2086 rows × 5 columns


In [23]:
#Merging all the dataframes to a mother dataframe
m_rent = pd.merge(rdata_2000, rdata_2013, how='outer', on=['GEO.id', 'GEO.id2', 'GEO.display-label'])
m_rent


Out[23]:
GEO.id GEO.id2 GEO.display-label VD01 HD01_VD01 HD02_VD01
0 Id Id2 Geography Median gross rent Estimate; Median gross rent Margin of Error; Median gross rent
1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 750 1867 1053
2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 671 1450 315
3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 904 1558 211
4 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 835 1228 122
5 1500000US360470002002 360470002002 Block Group 2, Census Tract 2, Kings County, N... 690 NaN NaN
6 1500000US360470003011 360470003011 Block Group 1, Census Tract 3.01, Kings County... 1000 2,000+ ***
7 1500000US360470003012 360470003012 Block Group 2, Census Tract 3.01, Kings County... 1046 1535 366
8 1500000US360470003013 360470003013 Block Group 3, Census Tract 3.01, Kings County... 1075 2,000+ ***
9 1500000US360470003014 360470003014 Block Group 4, Census Tract 3.01, Kings County... 863 1795 165
10 1500000US360470003015 360470003015 Block Group 5, Census Tract 3.01, Kings County... 843 - **
11 1500000US360470003021 360470003021 Block Group 1, Census Tract 3.02, Kings County... 1150 NaN NaN
12 1500000US360470005001 360470005001 Block Group 1, Census Tract 5, Kings County, N... 988 NaN NaN
13 1500000US360470005002 360470005002 Block Group 2, Census Tract 5, Kings County, N... 889 NaN NaN
14 1500000US360470005003 360470005003 Block Group 3, Census Tract 5, Kings County, N... 1123 NaN NaN
15 1500000US360470005004 360470005004 Block Group 4, Census Tract 5, Kings County, N... 498 NaN NaN
16 1500000US360470005005 360470005005 Block Group 5, Census Tract 5, Kings County, N... 943 NaN NaN
17 1500000US360470007001 360470007001 Block Group 1, Census Tract 7, Kings County, N... 1139 1733 128
18 1500000US360470007002 360470007002 Block Group 2, Census Tract 7, Kings County, N... 1077 1944 235
19 1500000US360470007003 360470007003 Block Group 3, Census Tract 7, Kings County, N... 954 2,000+ ***
20 1500000US360470009001 360470009001 Block Group 1, Census Tract 9, Kings County, N... 851 2,000+ ***
21 1500000US360470009002 360470009002 Block Group 2, Census Tract 9, Kings County, N... 1164 2,000+ ***
22 1500000US360470011001 360470011001 Block Group 1, Census Tract 11, Kings County, ... 701 2,000+ ***
23 1500000US360470013001 360470013001 Block Group 1, Census Tract 13, Kings County, ... 1107 1765 438
24 1500000US360470013002 360470013002 Block Group 2, Census Tract 13, Kings County, ... 825 - **
25 1500000US360470018001 360470018001 Block Group 1, Census Tract 18, Kings County, ... 950 - **
26 1500000US360470020001 360470020001 Block Group 1, Census Tract 20, Kings County, ... 758 1321 89
27 1500000US360470020002 360470020002 Block Group 2, Census Tract 20, Kings County, ... 683 1318 794
28 1500000US360470020003 360470020003 Block Group 3, Census Tract 20, Kings County, ... 644 NaN NaN
29 1500000US360470021001 360470021001 Block Group 1, Census Tract 21, Kings County, ... 950 2,000+ ***
... ... ... ... ... ... ...
2275 1500000US360471104001 360471104001 Block Group 1, Census Tract 1104, Kings County... NaN 1591 399
2276 1500000US360471104002 360471104002 Block Group 2, Census Tract 1104, Kings County... NaN 510 135
2277 1500000US360471104003 360471104003 Block Group 3, Census Tract 1104, Kings County... NaN 1232 136
2278 1500000US360471104004 360471104004 Block Group 4, Census Tract 1104, Kings County... NaN 1569 891
2279 1500000US360471106002 360471106002 Block Group 2, Census Tract 1106, Kings County... NaN 621 32
2280 1500000US360471110002 360471110002 Block Group 2, Census Tract 1110, Kings County... NaN 422 193
2281 1500000US360471116001 360471116001 Block Group 1, Census Tract 1116, Kings County... NaN 1362 138
2282 1500000US360471116002 360471116002 Block Group 2, Census Tract 1116, Kings County... NaN 1352 565
2283 1500000US360471144001 360471144001 Block Group 1, Census Tract 1144, Kings County... NaN 1683 169
2284 1500000US360471144002 360471144002 Block Group 2, Census Tract 1144, Kings County... NaN 1435 207
2285 1500000US360471144003 360471144003 Block Group 3, Census Tract 1144, Kings County... NaN 1365 301
2286 1500000US360471144004 360471144004 Block Group 4, Census Tract 1144, Kings County... NaN 576 19
2287 1500000US360471190002 360471190002 Block Group 2, Census Tract 1190, Kings County... NaN 1321 153
2288 1500000US360471198001 360471198001 Block Group 1, Census Tract 1198, Kings County... NaN 1223 375
2289 1500000US360471198002 360471198002 Block Group 2, Census Tract 1198, Kings County... NaN 1034 154
2290 1500000US360471198003 360471198003 Block Group 3, Census Tract 1198, Kings County... NaN 1299 415
2291 1500000US360471208004 360471208004 Block Group 4, Census Tract 1208, Kings County... NaN 1060 125
2292 1500000US360471208005 360471208005 Block Group 5, Census Tract 1208, Kings County... NaN 1230 331
2293 1500000US360471210002 360471210002 Block Group 2, Census Tract 1210, Kings County... NaN 563 42
2294 1500000US360471214002 360471214002 Block Group 2, Census Tract 1214, Kings County... NaN 338 62
2295 1500000US360471237001 360471237001 Block Group 1, Census Tract 1237, Kings County... NaN 1245 125
2296 1500000US360471237002 360471237002 Block Group 2, Census Tract 1237, Kings County... NaN 1415 801
2297 1500000US360471237003 360471237003 Block Group 3, Census Tract 1237, Kings County... NaN 1588 297
2298 1500000US360471502001 360471502001 Block Group 1, Census Tract 1502, Kings County... NaN 1754 276
2299 1500000US360471502002 360471502002 Block Group 2, Census Tract 1502, Kings County... NaN 2,000+ ***
2300 1500000US360471502003 360471502003 Block Group 3, Census Tract 1502, Kings County... NaN 1204 513
2301 1500000US360471522001 360471522001 Block Group 1, Census Tract 1522, Kings County... NaN 1540 198
2302 1500000US360471522002 360471522002 Block Group 2, Census Tract 1522, Kings County... NaN 1353 192
2303 1500000US360471522003 360471522003 Block Group 3, Census Tract 1522, Kings County... NaN 2,000+ ***
2304 1500000US360479901000 360479901000 Block Group 0, Census Tract 9901, Kings County... NaN - **

2305 rows × 6 columns


In [24]:
#Saving the dataset as csv
m_rent.to_csv('m_rent.csv')

In [25]:
#Checking the CSV
data = pd.read_csv('m_rent.csv')
data.head()


Out[25]:
Unnamed: 0 GEO.id GEO.id2 GEO.display-label VD01 HD01_VD01 HD02_VD01
0 0 Id Id2 Geography Median gross rent Estimate; Median gross rent Margin of Error; Median gross rent
1 1 1500000US360470001001 360470001001 Block Group 1, Census Tract 1, Kings County, N... 750 1867 1053
2 2 1500000US360470001002 360470001002 Block Group 2, Census Tract 1, Kings County, N... 671 1450 315
3 3 1500000US360470001003 360470001003 Block Group 3, Census Tract 1, Kings County, N... 904 1558 211
4 4 1500000US360470002001 360470002001 Block Group 1, Census Tract 2, Kings County, N... 835 1228 122

Good to go!


In [ ]: