df = pd.read_csv('path', thousands=',', encoding='euc-kr')
crime_station.columns = crime_station.columns.droplevel([0,1])
crime_station.columns
gmaps = googlemaps.Client(key = gmaps_key)
temp = gmaps.geocode('위치 지정!', language = 'ko')
temp[0].get('formatted_address')['location']['lat']
for idx, row in crime_station.iterrows():
#print(type(idx))
station_name = '서울' + idx + '경찰서'
print(station_name)
temp = gmaps.geocode(station_name, language='ko')
#print(temp)
temp[0].get('formatted_address')
temp_gu = temp[0].get('formatted_address').split() # 찾아야될거.있는위치
lat = temp[0].get('geometry')['location']['lat']
lng = temp[0].get('geometry')['location']['lng']
crime_station.loc[idx,'lat'] = lat
crime_station.loc[idx,'lng'] = lng
if len(temp_gu) >=3:
crime_station.loc[idx,'구별'] = temp_gu[2]
else:
print(f'이상한 것 : {idx}')
continue
crime_station
crime_station.columns.get_level_values(0)
tmp = [
crime_station.columns.get_level_values(0))[n]
+ crime_station.columns.get_level_values(1)[n]
for n in range(0, len(crime_station.columns.get_level_values(0))
]
crime_anal_station = pd.read_csv(
'../data/02. my_crime_in_Seoul_raw.csv', index_col=0, encoding='utf-8'
)
crime_anal_gu[['강도검거','살인검거']].div(crime_anal_gu['강도발생'], axis = 0).head()
num = ['강간검거', '강도검거', '살인검거','절도검거','폭력검거']
den = ['강간발생', '강도발생', '살인발생','절도발생','폭력발생']
crime_anal_gu[num].div(crime_anal_gu[den].values)
crime_anal_gu[num] / crime_anal_gu[den].values
def drawGraph():
sns.pairplot(
data=crime_anal_norm,
x_vars=['인구수','CCTV'],
y_vars=['살인','강도'],
kind='reg',
height=4
)
plt.show()
drawGraph()
e.g)
# 5대 범죄 검거 건수 지도 시각화
my_map = folium.Map(
location =[37.5502,126.982],
zoom_start = 11,
tiles = 'Stamen Toner'
)
folium.Choropleth(
geo_data = geo_str,
data = crime_anal_norm,
columns=[crime_anal_norm.index, '검거'],
key_on = 'feature.id',
fill_color = 'PuRd',
fill_opacity=0.7,
line_opacity=0.2,
legend_name='정규화된 범죄 발생 건수'
).add_to(my_map)
my_map
my_map = folium.Map(
location = [37.5502,126.982],
zoom_start= 11
)
folium.Choropleth(
geo_data = geo_str,
data = crime_anal_norm,
columns=[crime_anal_norm.index, '범죄'],
key_on = 'feature.id',
fill_color = 'PuRd',
fill_opacity=0.7,
line_opacity=0.2,
).add_to(my_map)
for idx, rows in crime_anal_station.iterrows():
folium.CircleMarker(
location=[rows['lat'],rows['lng']],
radius = rows['검거'] * 50,
popup=rows['구분'] + ':'+'%.2f'%rows['검거'],
color='#3186cc',
fill=True,
fill_color='#3186cc'
).add_to(my_map)
my_map
crime_loc_norm_sort = crime_loc_norm.sort_values('총합', ascending=False)
def drawGraph():
plt.figure(figsize=(10,10))
sns.heatmap(
crime_loc_norm_sort,
annot = True,
fmt = 'f',
linewidths=0.5,
cmap='RdPu'
)
plt.title('범죄발생장소')
plt.show()
drawGraph()