-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcensusapi.py
124 lines (95 loc) · 3.53 KB
/
censusapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from census import Census
from datetime import datetime
# API_KEY = "32e797a32ba6e8830a02ac4a88804ec08124e470"
# c = Census(API_KEY)
def get_med_inc_nonfamily(zipcode):
"""
Returns the median income in a given zip code for
non-family (1-2) person households.
Requires:
- zipcode to be the string form of a valid zipcode.
Returns:
- An integer representing the median income for Nonfamily households.
- -1 if input invalid or other error occurred
"""
API_KEY = "32e797a32ba6e8830a02ac4a88804ec08124e470"
c = Census(API_KEY)
# The table lookup corresponding to the data that we want.
table_code = 'B19202_001E'
# Return value is a list of dictionary.
api_return = c.acs5.zipcode((table_code), zipcode, state_fips=0)
# If we have an invalid entry, give average for US.
if (len(api_return) == 0):
api_return = c.acs5.us((table_code))
# Unwrap the api value
ret = api_return[0][table_code]
return ret
def get_med_inc_family(zipcode):
"""
Returns the median income in a given zip code for
family (3+) person households.
Requires:
- zipcode to be the string form of a valid zipcode.
Returns:
- An integer representing the median income for households.
- -1 if input invalid or other error occurred
"""
API_KEY = "32e797a32ba6e8830a02ac4a88804ec08124e470"
c = Census(API_KEY)
# The table lookup corresponding to the data that we want.
table_code = 'B19013_001E'
# Return value is a list of dictionary.
api_return = c.acs5.zipcode((table_code), zipcode, state_fips=0)
# If we have an invalid zipcode, give average for US.
if (len(api_return) == 0):
api_return = c.acs5.us((table_code))
# Unwrap the api value
ret = api_return[0][table_code]
return ret
def get_med_rent(zipcode):
"""
Returns the median rent in a given zipcode.
Requires:
- zipcode to be the string form of a valid zipcode.
Returns:
- An integer representing the median rent.
- -1 if input invalid or other error occurred
"""
API_KEY = "32e797a32ba6e8830a02ac4a88804ec08124e470"
c = Census(API_KEY)
# The table lookup corresponding to the data that we want.
table_code = 'B25058_001E'
# Return value is a list of dictionary.
api_return = c.acs5.zipcode((table_code), zipcode, state_fips=0)
# If we have an invalid zipcode, give average for US.
if (len(api_return) == 0):
api_return = c.acs5.us((table_code))
# Unwrap the api value
ret = api_return[0][table_code]
return ret
def get_med_housing_cost(zipcode):
"""
Returns the median housing cost in a given zipcode.
Requires:
- zipcode to be the string form of a valid zipcode.
Returns:
- An integer representing the median housing cost.
- -1 if input invalid or other error occurred
"""
API_KEY = "32e797a32ba6e8830a02ac4a88804ec08124e470"
c = Census(API_KEY)
# The table lookup corresponding to the data that we want.
table_code = 'B25105_001E'
# Return value is a list of dictionary.
api_return = c.acs5.zipcode((table_code), zipcode, state_fips=0)
# If we have an invalid zipcode, give average for US.
if (len(api_return) == 0):
api_return = c.acs5.us((table_code))
# Unwrap the api value
ret = api_return[0][table_code]
return ret
#
# print(get_med_inc_nonfamily('02446'))
# print(get_med_inc_family('02446'))
# print(get_med_rent('02446'))
# print(get_med_housing_cost('02446'))