-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreatingAllDataFile.py
292 lines (232 loc) · 8.94 KB
/
CreatingAllDataFile.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
287
288
289
import string
from shapely.wkt import loads
import geopandas as gp
import pandas as p
from shapely.geometry import Point
from sklearn import tree
import ReadingCoord
import matplotlib.pyplot as plt
import geodatasets
import numpy as np
from shapely import wkt
from shapely.errors import WKTReadingError
from lxml import etree
import xml.etree.ElementTree as ET
print("hello")
df = p.read_csv('GroceryStore.csv')
#put an r in front of the filepath so you don't have to do double backslashes
#Ex: df2=p.read_csv(r'C:\Users\venki\VSCode\Python\ChicagoSummerProject\ChicagoSummerProj\database\Business_License.csv')
df2 = p.read_csv("database\\Business_License.csv")
#use nrows to limit number of rows
housing = p.read_csv(r"database\Affordable_Rental_Housing_Developments_20240529.csv")
pages = p.read_html('https://en.wikipedia.org/wiki/Community_areas_in_Chicago#cite_note-CMAP_Population-11')
#print(pages)
hospitals = p.read_csv(r"database\Public_Health_Services-_Chicago_Primary_Care_Community_Health_Centers.csv")
schools=p.read_csv(r"database\CPS_School_Locations_SY1415_20240717.csv")
chicago = ET.parse("ChicagoMap.xml")
root = chicago.getroot()
allData=p.read_csv(r"AllDataFile.csv")
census=p.read_csv(r"database\Census_Data_-_Selected_socioeconomic_indicators_in_Chicago__2008___2012_20240529.csv")
crime=p.read_csv(r"database\Crime.csv")
x=(root[0][1].find("the_geom"))
print(type(wkt.loads(x)))
'''for community in root.iter("community"):
x=(census[census["COMMUNITY AREA NAME"] == string.capwords(community.text)].index.tolist())
if(x==[]):
print(community.text)'''
'''print(census.loc[4, "COMMUNITY AREA NAME"].upper())
print(chicago.iloc[4])
print(type(census.loc[6, "COMMUNITY AREA NAME"]))
value=chicago[chicago["COMMUNITY"] == census.loc[8, "COMMUNITY AREA NAME"].upper()].index.tolist() #use .loc directly for any content value, but use .apply(loads) and then .loc[] for points
'''
df['points'] = df['Location'].apply(loads)
df2["points2"] = df2.apply(lambda col: Point(col.LONGITUDE, col.LATITUDE), axis=1)
housing["points"] = housing.apply(lambda col: Point(col.Longitude, col.Latitude), axis=1)
schools["points"] = schools["the_geom"].apply(loads)
crime["points"] = crime.apply(lambda col: Point(col.LONGITUDE, col.LATITUDE), axis=1)
#print(wkt.loads(chicago.loc[8, "the_geom"]))
'''try:
for x in range(len(chicago["the_geom"])):
wkt.loads(chicago.loc[x, "the_geom"])
except WKTReadingError as e:
print(x)'''
listVal = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listVal.append(0)
listVal2 = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listVal2.append(0)
listCE = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listCE.append(0)
listCH = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listCH.append(0)
listDE = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listDE.append(0)
listDH = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listDH.append(0)
listOE = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listOE.append(0)
listOH = []
for x in range(len(census["COMMUNITY AREA NAME"])):
listOH.append(0)
#Businesses
'''value=0
for community in root.iter("community"):
index=(census[census["COMMUNITY AREA NAME"] == string.capwords(community.text)].index.tolist())
geom=root[0][value].find("the_geom").text
count=0
if(index==[]):
listVal.append(0)
else:
for y in range(len(df2["points2"])):
if(wkt.loads(geom).contains(df2.loc[y, "points2"])):
count+=1
listVal[index[0]]=count
value+=1
print(index, ":", count)
allData["Business Count"]=listVal'''
#allData.to_csv(r"C:\Users\venki\VSCode\Python\ChicagoSummerProject\ChicagoSummerProj\AllDataFile.csv", index=False)
#Housing
'''value=0
for community in root.iter("community"):
index=(census[census["COMMUNITY AREA NAME"] == string.capwords(community.text)].index.tolist())
geom=root[0][value].find("the_geom").text
count=0
countU=0
if(index==[]):
listVal.append(0)
else:
for y in range(len(housing["points"])):
if(wkt.loads(geom).contains(housing.loc[y, "points"])):
count+=1
countU+=int(housing.loc[y,"Units"])
listVal[index[0]]=count
listVal2[index[0]]=countU
value+=1
print(index, ":", count, countU)
allData["Housing Count"]=listVal
allData["Housing Count Units"]=listVal2'''
#allData.to_csv(r"C:\Users\venki\VSCode\Python\ChicagoSummerProject\ChicagoSummerProj\AllDataFile.csv", index=False)
#Hospitals
'''value=0
pointsHos=hospitals["Address"]
lats=[]
longs=[]
for x in range(len(pointsHos)):
add=hospitals.loc[x, "Address"]
lat=add[add.index("(")+1 : add.index(",", add.index("("))]
lats.append(float(lat))
long=add[add.index(" ", add.index("("))+1 : add.index(")")]
longs.append(float(long))
hospitals["lat"]=lats
hospitals["long"]=longs
hospitals["points"]=hospitals.apply(lambda col: Point(col.long, col.lat), axis=1)
for community in root.iter("community"):
index=(census[census["COMMUNITY AREA NAME"] == string.capwords(community.text)].index.tolist())
geom=root[0][value].find("the_geom").text
count=0
if(index==[]):
listVal.append(0)
else:
for y in range(len(hospitals["points"])):
if(wkt.loads(geom).contains(hospitals.loc[y, "points"])):
count+=1
listVal[index[0]]=count
value+=1
allData["Hospital Count"]=listVal'''
#allData.to_csv(r"C:\Users\venki\VSCode\Python\ChicagoSummerProject\ChicagoSummerProj\AllDataFile.csv", index=False)
#Schools
'''value=0
for community in root.iter("community"):
index=(census[census["COMMUNITY AREA NAME"] == string.capwords(community.text)].index.tolist())
geom=root[0][value].find("the_geom").text
chartE=0
chartH=0
distE=0
distH=0
otherE=0
otherH=0
for y in range(len(schools["points"])):
if(wkt.loads(geom).contains(schools.loc[y, "points"])):
if(schools.loc[y, "SCH_TYPE"]=="Charter"):
if(schools.loc[y, "GRADE_CAT"]=="ES"):
chartE+=1
else:
chartH+=1
elif(schools.loc[y, "SCH_TYPE"]=="District"):
if(schools.loc[y, "GRADE_CAT"]=="ES"):
distE+=1
else:
distH+=1
else:
if(schools.loc[y, "GRADE_CAT"]=="ES"):
otherE+=1
else:
otherH+=1
if(index[0]==0):
print(schools.loc[y,"the_geom"])
listCE[index[0]]=chartE
listCH[index[0]]=chartH
listDE[index[0]]=distE
listDH[index[0]]=distH
listOE[index[0]]=otherE
listOH[index[0]]=otherH
value+=1
allData["Charter School Elementary Count"]=listCE
allData["Charter School High School Count"]=listCH
allData["District School Elementary Count"]=listDE
allData["District School High School Count"]=listDH
allData["ALOP/Safe School Elementary Count"]=listOE
allData["ALOP/Safe School High School Count"]=listOH'''
#allData.to_csv(r"AllDataFile.csv", index=False)
#Crime
'''value=0
for community in root.iter("community"):
index=(census[census["COMMUNITY AREA NAME"] == string.capwords(community.text)].index.tolist())
geom=root[0][value].find("the_geom").text
count=0
if(index==[]):
listVal.append(0)
else:
for y in range(len(crime["points"])):
if(wkt.loads(geom).contains(crime.loc[y, "points"])):
count+=1
listVal[index[0]]=count
print(index, ":", count)
value+=1
allData["Crime Count"]=listVal
allData.to_csv(r"AllDataFile.csv", index=False)'''
#Grocery stores
'''value=0
for community in root.iter("community"):
index=(census[census["COMMUNITY AREA NAME"] == string.capwords(community.text)].index.tolist())
geom=root[0][value].find("the_geom").text
count=0
if(index==[]):
listVal.append(0)
else:
for y in range(len(df["points"])):
if(wkt.loads(geom).contains(df.loc[y, "points"])):
count+=1
listVal[index[0]]=count
print(index, ":", count)
value+=1
allData["Grocery Stores"]=listVal
allData.to_csv(r"AllDataFile.csv", index=False)'''
allData["%_OF_HOUSING_CROWDED"] = census["PERCENT OF HOUSING CROWDED"]
allData.to_csv(r"AllDataFile.csv", index=False)
allData["%_OF_HOUSEHOLDS_BELOW_POVERTY"] = census["PERCENT HOUSEHOLDS BELOW POVERTY"]
allData.to_csv(r"AllDataFile.csv", index=False)
allData["%_AGED_16+_UNEMPLOYED"] = census["PERCENT AGED 16+ UNEMPLOYED"]
allData.to_csv(r"AllDataFile.csv", index=False)
allData["%_AGED_25+_WITHOUT_HIGH_SCHOOL_DIPLOMA"] = census["PERCENT AGED 25+ WITHOUT HIGH SCHOOL DIPLOMA"]
allData.to_csv(r"AllDataFile.csv", index=False)
allData["PERCENT_AGED_UNDER_18_OR_OVER_64"] = census["PERCENT AGED UNDER 18 OR OVER 64"]
allData.to_csv(r"AllDataFile.csv", index=False)
allData["PER_CAPITA_INCOME"] = census["PER CAPITA INCOME "]
allData.to_csv(r"AllDataFile.csv", index=False)