πŸ‘©β€βš•οΈ λ‚˜μ™€ κ°€κΉŒμš΄ μ˜λ£ŒκΈ°κ΄€ μ°ΎκΈ° πŸ‘¨β€βš•οΈ

parkeuΒ·2022λ…„ 9μ›” 8일
0

ABCλΆ€νŠΈμΊ ν”„

λͺ©λ‘ 보기
19/55

🐼 μ€€λΉ„

# ν•œκΈ€κΉ¨μ§ 방지
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. μ˜μ—…μ€‘μΈ 병원 쀑 λ‚΄ μœ„μΉ˜μ™€ κ°€μž₯ κ°€κΉŒμš΄ μ˜λ£ŒκΈ°κ΄€ 5개 μ°ΎκΈ°
  2. λ‚΄ μœ„μΉ˜λŠ” λ„λ‘œλͺ… μ£Όμ†Œλ‘œ μž…λ ₯λ°›μŒ
  3. λ‚΄ μœ„μΉ˜ λ§ˆμ»€μ™€ 병원 마컀λ₯Ό ν‘œμ‹œν•˜κ³  병원 마컀λ₯Ό ν΄λ¦­ν•˜λ©΄ 병원λͺ…이 λ‚˜μ˜€λ„λ‘ μ‹œκ°ν™”
# 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 μž…λ ₯, 병원 λ§ˆμ»€λŠ” νŒŒλž€μƒ‰ ν”ŒλŸ¬μŠ€λ‘œ ν‘œμ‹œ


🚩 μ†Œκ° 🚩

κ³΅λΆ€ν•˜λ©΄ ν• μˆ˜λ‘ λͺ¨λ₯΄λŠ”κ²Œ 정말 많고 곡뢀할 것도 많고 λ‚΄μš©λ„ μ–΄λ €μ›Œμ§€λŠ” 것 κ°™λ‹€. κ·Έλž˜λ„ 이해 μ•ˆ λ˜λŠ” 뢀뢄은 κ²€μƒ‰ν•˜λ©΄ λ§Žμ€ μžλ£Œκ°€ 있고 책도 μ°Έκ³ ν•  책이 λ§Žμ•„μ„œ μ’‹κΈ΄ν•œλ° 이것도 ν‚€μ›Œλ“œλ₯Ό 잘 λ½‘μ•„μ„œ κ²€μƒ‰ν•΄μ•Όλ˜λŠ”λ“― γ…Žγ…Ž.. 지도 μ‹œκ°ν™”λ³΄λ‹€λŠ” 크둀링이 더 μž¬λ°ŒλŠ”κ²ƒκ°™λ‹€ ^^...이번 μ—°νœ΄μ•ˆμ— μˆ˜μš”μΌκΉŒμ§€ κ³΅λΆ€ν•œ λ‚΄μš© λ‹€ μ •λ¦¬ν•˜κ³ λ§κ²Œμ¨,,,

profile
배고파용.

0개의 λŒ“κΈ€