folium&wordcloud
category
URL
date
Jan 31, 2023
slug
folium-wordcloud
author
status
Public
tags
Playdata_Python
summary
folium 및 wordcloud 내용
type
Post
thumbnail
updatedAt
Jan 31, 2023 08:22 AM
folium
- 파이썬의 라이브러리로 지도 데이터를 시각화하는데 쉽게 도와줌
- leaflet.js를 기반으로 만들어짐
- 지도에 마커를 표현하거나 범위를 나타내는 다양한 도형을 입력
conda에 folium 설치하기
→ 아나콘다 프롬포트
→ conda install -c conda-forge folium 입력
jupyter lab에서 folium import하기
→ import folium
플레이데이터 지도에 표시
# 플레이데이터 독산 위도, 경도
latitude, longitude = (37.468251, 126.886212)
# 해당 위도,경도 정보를 바탕으로 지도에 표시
# Marker를 달아 위치에 대한 정보에 '플레이데이터'라고 표기
m = folium.Map(location = [latitude, longitude],
zoom_start=17,
width=750,
height=500
)
folium.Marker([latitude, longitude],
popup = "플레이데이터",
tooltip="플레이데이터").add_to(m)
m

import pandas as pd
import numpy as np
survey_raw_df = pd.read_csv('C:/python/data/survey_results_public.csv',index_col="ResponseId")
countries_geojson = 'https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json'
# 지도에 활용할 DataFrame 만들기
country_counts = survey_raw_df.Country.value_counts()
country_counts_df = pd.DataFrame({'Country' : country_counts.index,
'Count' : country_counts.values})
country_counts_df
# 데이터 가공
# json 속성 feature.properties.name의 값과 우리가 csv에서 가져온 country name이 일치
# 이중 Russian Federation의 값이 불일치. 이를 Russia로 변경
country_counts_df.at[12,'Country']
country_counts_df.at[12,'Country'] = 'Russia'
# Choropleth는 데이터를 담고 있는 Pandas DataFrame / Series와 기하학 데이터를 담는 Geo/TopoJSON를 바인딩하여 쉽게 시각화 표현 할 수 있도록 돕습니다.
m = folium.Map(location=[30,0], zoom_start = 2)
folium.Choropleth(
geo_data=countries_geojson,
data=country_counts_df,
columns=["Country", "Count"],
key_on = "feature.properties.name",
threshold_scale=[1, 30, 100, 300, 1_100, 3_000, 10_000, 14_000],
fill_color="YlGn",
fill_opacity=0.7,
line_opacity=0.2,
legend_name = "Respondents",
).add_to(m)
folium.LayerControl().add_to(m)
m
# save() 메서드 사용하여 저장
m.save("Country.html")
# 서울시 행정구역 geojson
seoul_geojson="https://raw.githubusercontent.com/southkorea/seoul-maps/master/kostat/2013/json/seoul_municipalities_geo_simple.json"
m = folium.Map(
location=[37.57, 126.99],
zoom_start=11,
)
folium.Choropleth(
geo_data=seoul_geojson,
fill_color="#22AA44",
fill_opacity=0.4,
line_opacity=1,
).add_to(m)
m
# 더 많은 정보 확인
https://python-visualization.github.io/folium/quickstart.html
WordCloud
→ 설치 pip install -c conda-forge wordcloud
conda install -c conda-forge wordcloud
from wordcloud import WordCloud
import pandas as pd
import numpy as np
survey_df = pd.read_csv('C:/python/data/survey_results_public.csv',index_col="ResponseId")
# 데이터 가공
# DevType에 있는 모든 NaN값 제거
survey_df.DevType
temp_words = survey_df.DevType.dropna()
temp_words
temp_words = temp_words.str.replace(';',' ').str.replace(',',' ')
temp_words
words = ' '.join(temp_words)
words