본문 바로가기

Python

Python - 지도 (folium) 연습문제

1. 다음 지도가 나오도록 프로그램을 작성하기

US_Unemployment_Oct2012.csv
0.00MB
us-states.json
0.08MB

import pandas as pd
import folium

state_geo = 'us-states.json'
state_unemployment = 'US_Unemployment_Oct2012.csv'
state_data = pd.read_csv(state_unemployment)
m = folium.Map(location=[48,-102], zoom_start=3, tiles='Stamen Toner')

folium.Choropleth(
    state_geo,
    data=state_data,
    columns=["State","Unemployment"],
    key_on='feature.id',
    fill_color='YlGn',
    fill_opacity=0.7,
    line_opacity=0.2,
    legend_name="Unemployment Rate (%)",
).add_to(m)

m.save('usa1.html')

 

2. 서울시 범죄율 데이터를 이용하여 다음 지도를 작성하기

crime_in_Seoul_final.csv
0.01MB
skorea_municipalities_geo_simple.json
0.01MB

import json
import pandas as pd
import folium

geo_path = "skorea_municipalities_geo_simple.json"
geo_str = json.load(open(geo_path, encoding='utf-8'))
geo_str 

maps_korea = folium.Map(location=[37.5502,126.982], zoom_start=11)
df = pd.read_csv("crime_in_Seoul_final.csv")
df = df.set_index("구별")
df.head()

maps_korea.choropleth(geo_data=geo_str, data=df["살인"],
                      columns=[df.index, df["살인"]],
                      fill_color='YlOrRd', fill_opacity=0.5, line_opacity=0.2,
                      key_on="feature.properties.name"
        )
maps_korea.save('maps_korea1.html')