This repository has been archived by the owner on Dec 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.py
287 lines (243 loc) · 10.2 KB
/
api.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import requests
from requests.auth import HTTPBasicAuth
import pandas
from census import Census
from us import states
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
plotly.tools.set_credentials_file(username='manika15', api_key='dogNq5ILvZnKd5BzKOry')
#api key to access data using us census api (acs5)
apikey = "d8fa9f7c0841efecfb91b98bf8cbe056cf654cec"
#url to access
request_url = "http://citysdk.commerce.gov"
#function to fetch share of households with income >150k in Silicon Valley (San Mateo & santa Clara county
def sm_sc_income(year):
c = Census("d8fa9f7c0841efecfb91b98bf8cbe056cf654cec")
# household with income >150K and <200k in san mateo county
san_mateo1 = c.acs5.state_county('B19001_016E', states.CA.fips, '081', year=year)
# household with income >200k in san mateo county
san_mateo2 = c.acs5.state_county('B19001_017E', states.CA.fips, '081', year=year)
# households with income San Mateo
san_mateo_pop = c.acs5.state_county('B19051_002E', states.CA.fips, '081', year=year)
# household with income >150K and <200k in santa clara county
santa_clara1 = c.acs5.state_county('B19001_016E', states.CA.fips, '085', year=year)
# household with income >200k in santa clara county
santa_clara2 = c.acs5.state_county('B19001_017E', states.CA.fips, '085', year=year)
# households with income Santa clara
santa_clara_pop = c.acs5.state_county('B19051_002E', states.CA.fips, '085', year=year)
# share of households with income >150K in San Mateo & Santa clara
sm_tot = sum(int(item['B19001_016E']) for item in san_mateo1) + \
sum(int(item['B19001_017E']) for item in san_mateo2)
sc_tot = sum(int(item['B19001_016E']) for item in santa_clara1) + \
sum(int(item['B19001_017E']) for item in santa_clara2)
s_tot_pop = sum(int(item['B19051_002E']) for item in san_mateo_pop) + \
sum(int(item['B19051_002E']) for item in santa_clara_pop)
s_ratio = ((sm_tot + sc_tot) / s_tot_pop * 100)
s_ratio = round(s_ratio, 1)
return s_ratio
def sf_income(year):
c = Census("d8fa9f7c0841efecfb91b98bf8cbe056cf654cec")
# household with income >150K and <200k in san fransisco county
san_fransisco1 = c.acs5.state_county('B19001_016E', states.CA.fips, '075', year=year)
# household with income >200k in san fransisco county
san_fransisco2 = c.acs5.state_county('B19001_017E', states.CA.fips, '075', year=year)
# households with income San Fransisco
# sf_pop = c.acs5.state_county('B01003_001E', states.CA.fips, '075', year=year)
sf_pop = c.acs5.state_county('B19051_002E', states.CA.fips, '075', year=year)
# share of households with income >150K in San Fransisco
sf_tot = sum(int(item['B19001_016E']) for item in san_fransisco1) + \
sum(int(item['B19001_017E']) for item in san_fransisco2)
sf_tot_pop = sum(int(item['B19051_002E']) for item in sf_pop)
sf_ratio = (sf_tot / sf_tot_pop * 100)
sf_ratio = round(sf_ratio, 1)
return sf_ratio
def cali_income(year):
c = Census("d8fa9f7c0841efecfb91b98bf8cbe056cf654cec")
# household with income >150K and <200k in California
cali1 = c.acs5.state('B19001_016E', states.CA.fips, year=year)
# household with income >200k in California
cali2 = c.acs5.state('B19001_017E', states.CA.fips, year=year)
# households with income California
cali_pop = c.acs5.state('B19051_002E', states.CA.fips, year=year)
# share of households with income >150K in California
cali_tot = sum(int(item['B19001_016E']) for item in cali1) + \
sum(int(item['B19001_017E']) for item in cali2)
cali_tot_pop = sum(int(item['B19051_002E']) for item in cali_pop)
cali_ratio = ((cali_tot) / cali_tot_pop * 100)
cali_ratio = round(cali_ratio, 1)
return cali_ratio
def us_income(year):
c = Census("d8fa9f7c0841efecfb91b98bf8cbe056cf654cec")
# household with income >150K and <200k in US
us_tot1 = c.acs5.state('B19001_016E', Census.ALL, year=year)
# household with income >200k in US
us_tot2 = c.acs5.state('B19001_017E', Census.ALL, year=year)
#households with income US
us_pop = c.acs5.state('B19051_002E', Census.ALL, year=year)
#share of households with income >150K in US
us_tot= sum(int(item['B19001_016E']) for item in us_tot1) + \
sum(int(item['B19001_017E']) for item in us_tot2)
us_tot_pop = sum(int(item['B19051_002E']) for item in us_pop)
return us_tot_pop
#function to calculate share of households with income >150k
def share(income, population):
ratio = [round(a/b*100,1) for a,b in zip(income, population)]
return ratio
def income_150k_plot():
# households with income >150k in San Mateo county California
sm = [county_income(year, '081') for year in years]
# households with income >150k in Santa Clara county California
sc = [county_income(year, '085') for year in years]
# households with income >150k in Silicon Valley (San Mateo + Santa Clara) California
income_silicon = [a + b for a, b in zip(sm, sc)]
# households with income >150k in San Fransisco county California
income_sf = [county_income(year, '075') for year in years]
# households with income >150k in California state
income_cali = [cali_income(year) for year in years]
# households with income >150k in United States
income_us = [us_income(year) for year in years]
# households with income in San Mateo county California
smp = [county_households(year, '081') for year in years]
# households with income in Santa Clara county California
scp = [county_households(year, '085') for year in years]
# households with income in Silicon Valley (San Mateo + Santa Clara) California
population_silicon = [a + b for a, b in zip(smp, scp)]
# households with income in San Fransisco county California
population_sf = [county_households(year, '075') for year in years]
# households with income in California state
population_cali = [cali_population(year) for year in years]
# households with income in United States
population_us = [us_population(year) for year in years]
# share of households with income greater than 150k in Silicon valley
ratio_silicon = share(income_silicon, population_silicon)
# share of households with income greater than 150k in San Fransisco
ratio_sf = share(income_sf, population_sf)
# share of households with income greater than 150k in California state
ratio_cali = share(income_cali, population_cali)
# share of households with income greater than 150k in United states
ratio_us = share(income_us, population_us)
# ploting the Income graphs for Silicon valley, San Fransisco, California and United states
trace_silicon = go.Scatter(
x=years,
y=ratio_silicon,
name='Silicon valley',
line=dict(
color=('rgb(205, 12, 24)'),
width=4)
)
trace_sf = go.Scatter(
x=years,
y=ratio_sf,
name='San Fransisco',
line=dict(
color=('rgb(22, 96, 167)'),
width=4,
dash='dash')
)
trace_cali = go.Scatter(
x=years,
y=ratio_cali,
name='California',
line=dict(
color=('rgb(205, 12, 24)'),
width=4,
dash='dot')
)
trace_us = go.Scatter(
x=years,
y=ratio_us,
name='United States',
line=dict(
color=('rgb(22, 96, 167)'),
width=4)
)
data = [trace_silicon, trace_sf, trace_cali, trace_us]
layout = dict(title='Share of Households with Income >150k',
xaxis=dict(title='year', tickmode=years, nticks=5),
yaxis=dict(title='share of households', ticksuffix='%'),
width=1000,
height=450,
)
fig = dict(data=data, layout=layout)
py.plot(fig, filename='styled-line')
us_ratio = (us_tot / us_tot_pop *100)
return us_ratio
years = [2011, 2012, 2013, 2014, 2015]
#income for silicon valley from 2011 to 2015
income_silicon = [sm_sc_income(year) for year in years]
#income for San fransisco county from 2011 to 2015
income_sf = [sf_income(year) for year in years]
#income for California from 2011 to 2015
income_cali = [cali_income(year) for year in years]
#income for United States from 2011 to 2015
income_us = [us_income(year) for year in years]
trace_silicon = go.Scatter(
x = years,
y = income_silicon,
name = 'Silicon valley',
line = dict(
color = ('rgb(205, 12, 24)'),
width = 4)
)
trace_sf = go.Scatter(
x = years,
y = income_sf,
name = 'San Fransisco',
line = dict(
color = ('rgb(22, 96, 167)'),
width = 4,
dash='dash')
)
trace_cali = go.Scatter(
x = years,
y = income_cali,
name = 'California',
line = dict(
color = ('rgb(205, 12, 24)'),
width = 4,
dash='dot')
)
trace_us = go.Scatter(
x = years,
y = income_us,
name = 'United States',
line = dict(
color = ('rgb(22, 96, 167)'),
width = 4)
)
data = [trace_silicon, trace_sf, trace_cali, trace_us]
layout = dict(title = 'Share of Households with Income >150k',
xaxis = dict(title = 'year', tickmode= years, nticks=5),
yaxis = dict(title = 'share of households', ticksuffix= '%'),
width=1000,
height=450,
)
fig = dict(data=data, layout=layout)
py.plot(fig, filename='styled-line')
def main():
income_150k_plot()
# request_obj = {
# 'zip': '21401',
# 'state': 'MD',
# 'level': 'state',
# 'sublevel': False,
# 'api': 'acs5',
# 'year': 2010,
# 'variables': ['income', 'population']
# }
# response = requests.post(request_url, auth=HTTPBasicAuth(apikey, None), json=request_obj)
# data_gotten = response.json()
# print (data_gotten)
# types = data_gotten['type']
# features = data_gotten['features']
# totals = data_gotten['totals']
# panda_data = pandas.DataFrame(features)
#
# """
# pandas takes series data meaning a big array of stuffs not dicts so need
# to get array of data then stick into pandas for parsing i guesst
# """
if __name__ == "__main__":
main()