πΌ μ€λΉ
# νκΈκΉ¨μ§ λ°©μ§ import matplotlib as mpl import matplotlib.pyplot as plt %config InlineBackend.figure_format = 'retina' !apt -qq -y install fonts-nanum import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf' font = fm.FontProperties(fname=fontpath, size=9) plt.rc('font', family='NanumBarunGothic') mpl.font_manager._rebuild() # λΌμ΄λΈλ¬λ¦¬ μν¬νΈ import pandas as pd import plotly.express as px # λ°μ΄ν° μ€λΉ df = pd.read_csv('/content/data.csv', encoding='EUC-KR')
df['μμΈμμ
μνλͺ
'].value_counts()
gb_df = df.groupby(by=['μμΈμμ
μνλͺ
']).size().reset_index(name='μλ£κΈ°κ΄μ')
gb_df
π λͺ¨λ μμ
μ€μΈ μλ£κΈ°κ΄μ΄ μλλΌ νμ
λλ ν΄μ
ν μλ£κΈ°κ΄λ ν¬ν¨λ¨
# νμ€ν κ·Έλ¨ κ·Έλν
px.histogram(gb_df, x='μμΈμμ
μνλͺ
', y='μλ£κΈ°κ΄μ')
# νμ΄κ·Έλν
px.pie(gb_df, names='μμΈμμ
μνλͺ
', values='μλ£κΈ°κ΄μ')
π μλ£κΈ°κ΄λ€μ μμΈμμ
μν λ³ λΉμ¨μ νμ΄κ·Έλνμμ μ½κ² μμλ³Ό μ μλ€.
lgb_df = df.groupby(by=['μ§μλͺ
', 'λμλͺ
', 'μμΈμμ
μνλͺ
', 'μλ£κΈ°κ΄μ’
λ³λͺ
']).size()
lgb_df
π νκ΄μ λΆλΆμ΄ κ° κ·Έλ£Ήμ μ¬μ΄μ¦λ‘ size() κ²°κ³Όλ Series μ΄λΌλ 1μ°¨μ λ°°μ΄ μ€λΈμ νΈλ‘ λ°ν λ¨
lgb_df = df.groupby(by=['μ§μλͺ
', 'λμλͺ
', 'μμΈμμ
μνλͺ
', 'μλ£κΈ°κ΄μ’
λ³λͺ
']).size().reset_index()
lgb_df
π reset_indexλ‘ μΈλ±μ€ 컬λΌμ μλ£μ΄μ κ°μ₯ μ²μμ μ΄λ‘ μ€μ
lgb_df = df.groupby(by=['μ§μλͺ
', 'λμλͺ
', 'μμΈμμ
μνλͺ
', 'μλ£κΈ°κ΄μ’
λ³λͺ
']).size().reset_index(name='μλ£κΈ°κ΄μ')
lgb_df
π reset_indexμ nameμ ν΅ν΄ μΈλ±μ€ λͺ
μ§μ
set_indexμ reset_index
- reset_indexλ₯Ό κ²μνλ©΄ set_indexμ μ°κ΄λμ΄ μλ μ€λͺ μ΄ λ§μμ μ 리
set_index : κΈ°μ‘΄μ ν μΈλ±μ€λ₯Ό μ κ±°νκ³ λ°μ΄ν° μ΄ μ€ νλλ₯Ό μΈλ±μ€λ‘ μ€μ
reset_index : κΈ°μ‘΄μ ν μΈλ±μ€λ₯Ό μ κ±°νκ³ μΈλ±μ€λ₯Ό λ°μ΄ν° μ΄λ‘ μΆκ°set_index
df.set_index("μ΄λ¦")
π μμκ°μ λ°μ΄ν° νλ μμ set_index("μ΄λ¦")μ μ μ©νλ©΄, "μ΄λ¦" 컬λΌμ΄ μΈλ±μ€λ‘ μ§μ λ κ²μ νμΈν μ μλ€.
reset_index
df.reset_index()
π κ°μ λ°μ΄ν° νλ μμ reset_index()μ μ μ©νλ©΄, μλ‘μ΄ indexλΌλ 컬λΌμ΄ μμ±λ κ²μ λ³Ό μ μλ€.
# νμ€ν κ·Έλ¨ κ·Έλν
px.histogram(lgb_df, x='μ§μλͺ
', y='μλ£κΈ°κ΄μ')
# νμ΄κ·Έλν
px.pie(lgb_df, names='μ§μλͺ
', values='μλ£κΈ°κ΄μ')
px.histogram(lgb_df, x='μ§μλͺ
', y='μλ£κΈ°κ΄μ', color='μμΈμμ
μνλͺ
')
π color μ΅μ
μ μ£Όλ©΄ μκΉλ³λ‘ λνλΌ μ μλ€.
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
# μ§λ£κ³Όλͺ©λͺ
μ νλμ textννλ‘ λ³ν
text = " ".join(cont for cont in df.μ§λ£κ³Όλͺ©λ΄μ©λͺ
.astype(str))
text
# μλν΄λΌμ°λ μκ°ν
plt.subplots(figsize=(25, 15))
wordcloud = WordCloud(background_color='black', width=1000, height=700, font_path=fontpath).generate(text)
plt.axis('off')
plt.imshow(wordcloud, interpolation='bilinear')
plt.show()
π κΈμκ° ν΄μλ‘ λ§μ κ²
# 1) λΌμ΄λΈλ¬λ¦¬ μν¬νΈ
import folium
import pandas as pd
import plotly.express as px
from geopy.geocoders import Nominatim
from geopy.distance import geodesic
m = folium.Map(location=[36.3511, 127.3866], zoom_start=14)
m.add_child(folium.ClickForMarker(popup='point'))
m.add_child(folium.LatLngPopup())
# 2) dataframe μ€λΉ
data = pd.read_csv('/content/data.csv', encoding='EUC-KR')
# 3) μ£Όμλ₯Ό μ’νλ‘ λ³νν ν¨μ μ€λΉ
def geocoding(address):
geolocoder = Nominatim(user_agent = 'South Korea', timeout=None)
geo = geolocoder.geocode(address)
crd = {"lat": str(geo.latitude), "lng": str(geo.longitude)}
return crd
# 4) μ¬μ©μμκ² μ£Όμ μ
λ ₯λ°κΈ°
address = input('λλ‘λͺ
μ£Όμλ₯Ό μ
λ ₯νμΈμ : ')
crd = geocoding(address)
myhome = folium.Map(location=[crd['lat'], crd['lng']], zoom_start=14)
folium.Marker([crd['lat'], crd['lng']], icon=folium.Icon(color='red', icon='glyphicon glyphicon-home')).add_to(myhome)
my_hos = pd.DataFrame(columns=['λμλͺ
','μμΈμμ
μνλͺ
','μ¬μ
μ₯λͺ
','μλ','κ²½λ','거리'])
# 5) μ£Όμλ₯Ό μ’νλ‘ λ³ννμ¬ tuple ννλ‘ λ³μμ λ΄κΈ°
myhome = (crd['lat'], crd['lng'])
name = address.split(' ')[0]
data_name = data.loc[data.μ§μλͺ
.str.contains(name)]
for n in data_name.index:
hos_loc = (data.loc[n, 'μλ'], data.loc[n, 'κ²½λ']) # tuple
# dataframeμ 거리 κ³μ°νμ¬ λ΄κΈ°
my_hos.loc[n] = [data.loc[n,'λμλͺ
'], data.loc[n, 'μμΈμμ
μνλͺ
'], data.loc[n, 'μ¬μ
μ₯λͺ
'], data.loc[n, 'μλ'], data.loc[n, 'κ²½λ'], geodesic(myhome, hos_loc).kilometers]
my_hos = my_hos.loc[data_name.μμΈμμ
μνλͺ
.str.contains('μμ
μ€')]
my_hos = my_hos.sort_values(by=['거리']).head(5)
# 8) μ§λ μ€λΉ
my_map = folium.Map(location=[crd['lat'], crd['lng']], zoom_start=14)
# 9) λ§μ»€ νμ
for n in my_hos.index:
folium.Marker([my_hos.loc[n, 'μλ'], my_hos.loc[n, 'κ²½λ']], popup='<pre>'+my_hos.loc[n, 'μ¬μ
μ₯λͺ
']+'</pre>', icon=folium.Icon(icon='glyphicon glyphicon-plus')).add_to(my_map)
folium.Marker([crd['lat'], crd['lng']], icon=folium.Icon(color='red', icon='glyphicon glyphicon-home')).add_to(my_map)
my_map
π μμΈ κ°λ¨κ΅¬ κ°λ‘μκΈΈ 43 μ
λ ₯, λ³μ λ§μ»€λ νλμ νλ¬μ€λ‘ νμ
곡λΆνλ©΄ ν μλ‘ λͺ¨λ₯΄λκ² μ λ§ λ§κ³ 곡λΆν κ²λ λ§κ³ λ΄μ©λ μ΄λ €μμ§λ κ² κ°λ€. κ·Έλλ μ΄ν΄ μ λλ λΆλΆμ κ²μνλ©΄ λ§μ μλ£κ° μκ³ μ±
λ μ°Έκ³ ν μ±
μ΄ λ§μμ μ’κΈ΄νλ° μ΄κ²λ ν€μλλ₯Ό μ λ½μμ κ²μν΄μΌλλλ― γ
γ
.. μ§λ μκ°ν보λ€λ ν¬λ‘€λ§μ΄ λ μ¬λ°λκ²κ°λ€ ^^...μ΄λ² μ°ν΄μμ μμμΌκΉμ§ 곡λΆν λ΄μ© λ€ μ 리νκ³ λ§κ²μ¨,,,