πΌ μ€λΉ πΌ
- λλ‘κ΅ν΅κ³΅λ¨_κ΅ν΅μ¬κ³ μ 보.csvνμΌ μ΄μ©
import pandas as pd import plotly.express as px df = pd.read_csv('/content/λλ‘κ΅ν΅κ³΅λ¨_κ΅ν΅μ¬κ³ μ 보.csv', encoding='euc-kr')
β df.info() -> μ 보νμΈ
β plotly -> λ°μ΄ν° μκ°ν ν΄ https://plotly.com/
# λ°μλ
μμΌμ -> λ€μ λμ리(μκ°) λ°λ‘ 컬λΌμΌλ‘ λΆλ¦¬
# 1) λ°μλ
μμΌμ : μ°μμ μΈ μ«μ(int)μ΄λ―λ‘ μλ₯Ό μ μλ λ¬Έμμ΄(string)λ‘ λ³κ²½
df = df.astype({'λ°μλ
μμΌμ':'string'})
# 2) λ°μλ
μμΌμ -> λ€μ λμ리(μκ°) μλ₯΄κ³ λ°μμκ° μ΄λΌλ 컬λΌμ λ΄κΈ°
# ex) 2019010100 -> 00 λΆλ¦¬
df['λ°μμκ°'] = df['λ°μλ
μμΌμ'].str[8:]
# 3) λ°μμκ° μ»¬λΌ μ«μ λ°μ΄ν°νμ
μΌλ‘ λ³κ²½ -> strμ κ·Έλνλ₯Ό 그릴μμμΌλκΉ !
df = df.astype({'λ°μμκ°':'int64'})
# 4) λ°μλ
μμΌμ(string) -> λ μ§ νμ
μΌλ‘ λ³κ²½
# ex) 2019010100 -> 20190101(λ
μμΌ) μλ₯΄κ³ -> YYYY-MM-DD(λ μ§ ννλ‘ λ³κ²½)
df['λ°μλ
μμΌμ'] = pd.to_datetime(df['λ°μλ
μμΌμ'].str[:8], format='%Y-%m-%d', errors='raise')
β errors='raise' : μλ¬λ€μ΄ λ°μνλ©΄ μλ €μ£Όκ³ λ©μΆ°λΌ / 'ignore'μ΄λ©΄ 무μν΄λΌ
fig = px.scatter(df, x='λ°μλ
μμΌμ', y='λ°μμκ°', color='λ°μμ§μλ', hover_data=['λ°μμκ°'])
fig.show()
β fig = px.scatter(df, x='λ°μλ
μμΌμ', y='λ°μμκ°', color='λ°μμ§μλ', size='μ¬λ§μμ', hover_data=['λ°μμκ°'])
size ='μ¬λ§μμ' λ₯Ό μΆκ°νμ¬ λ²λΈμ°¨νΈ λͺ¨μμΌλ‘ 그릴 μ μλ€. !
fig = px.bar(df, x='μ¬λ§μμ', y='λ°μμκ°', orientation='h')
fig.show()
π 19μμ κ°μ₯ μ¬λ§μκ° λ§μμ μ μ μμ
# μΈλ‘μ°¨νΈ
fig = px.bar(df, x='λ°μμκ°', y='μ¬λ§μμ')
fig.show()
fig = px.bar(df, x='μ¬λ§μμ', y='λ°μμ§μλ', orientation='h')
fig.show()
# λ°μ΄ν° μ€λΉ
dj_df = df[df['λ°μμ§μλ'] =='λμ ']
# κ΅ν΅μ¬κ³ λ°μμκ°λ³ μ¬λ§μνν©
fig = px.bar(dj_df, x='μ¬λ§μμ', y='λ°μμκ°', orientation='h')
fig.show()
β μΈλ‘λͺ¨μ
fig = px.bar(dj_df, x='λ°μμκ°', y='μ¬λ§μμ')
fig.show()
fig = px.bar(dj_df, x='λ°μμ§μꡰꡬ', y='μ¬λ§μμ')
fig.show()
fig = px.bar(dj_df, x='μ¬λ§μμ', y='μμΌ', orientation='h')
fig.show()
fig = px.bar(dj_df, x='μ¬λ§μμ', y='μ¬κ³ μ ν', orientation='h')
fig.show()
import folium
map = folium.Map(location=[36.321665,127.378953]) # λμ μμ²μ μλ, κ²½λ κ°
# μ¬λ§μ¬κ³ μμΉμ CircleMarkerνμΆ
# CircleMarker size μ¬λ§μμ+κ²½μμμ μ€μ -> ν΄λ¦νλ©΄ νμ
μΌλ‘ μ 보(μ¬κ³ μ ν) λνλλλ‘ κ΅¬ν
# CircleMarker size(radius)λ μ€μν(float64)λ‘λ§ μ€μ κ°λ₯
# μ¬λ§μμ, κ²½μμμ int -> float64 λ°μ΄ν° νμ
λ³κ²½
dj_df = dj_df.astype({'μ¬λ§μμ' :'float64'})
dj_df = dj_df.astype({'κ²½μμμ' :'float64'})
β dj_df.info() κ²°κ³Ό
# forλ¬Έμ μ¬μ©νμ¬ dj_df λ°μ΄ν°κ° μμ λκΉμ§ λ°λ³΅μ μΌλ‘ CircleMarker μ§λμ add μν€λ μμ
-> 74λ²
for n in dj_df.index:
# CircleMarker μ¬μ΄μ¦ κ³μ°
cnt = dj_df['μ¬λ§μμ'][n] + dj_df['κ²½μμμ'][n]
folium.CircleMarker([dj_df['μλ'][n],dj_df['κ²½λ'][n]], radius =cnt*10, popup=dj_df['μ¬κ³ μ ν'][n],
color='#3186cc', fill_color='#3186cc').add_to(map)
β map.save('2019_λμ κ΅ν΅μ¬κ³ νν©.html') : htmlνμΌλ‘ μ μ₯
import pandas as pd
import plotly.express as px
df = pd.read_csv('/content/λλ‘κ΅ν΅κ³΅λ¨_μ¬λ§ κ΅ν΅μ¬κ³ μ 보_20211231.csv', encoding='euc-kr')
df.head()
# μκ°λ§ λ¨κΈ°κΈ°
# 2021-01-01 03:00μμ 03λ§ λ¨κΈ°κΈ°
date_split = df['λ°μλ
μμΌμ'].str.split(' ') # λ μ§μ μκ°:λΆ λΆλ¦¬
time_split = date_split.str.get(1) # μκ°:λΆλ§ μ ν
df['λ°μμκ°'] = time_split.str.split(':').str.get(0) # μκ°λ§ μ ν
df = df.astype({'λ°μμκ°':'int64'})
β df.info()
# λ°μλ
μμΌ μΆκ°
df['λ°μλ
μμΌ'] = pd.to_datetime(date_split.str.get(0), format='%Y-%m-%d', errors='raise')
β df.info()
fig = px.scatter(df, x='λ°μλ
μμΌμ', y='λ°μμκ°', color='λ°μμ§μλ', size='μ¬λ§μμ', hover_data=['λ°μμκ°'])
fig.show()
fig = px.scatter(df, x='λ°μλ
μμΌ', y='λ°μμκ°', color='λ°μμ§μλ', size='μ¬λ§μμ', hover_data=['λ°μμκ°'])
fig.show()
fig = px.bar(df, x='μ¬λ§μμ', y='λ°μμκ°', orientation='h')
fig.show()
fig = px.bar(df, x='λ°μμκ°', y='λ°μμ§μλ', orientation='h')
fig.show()
fig = px.bar(df, x='μ¬λ§μμ', y='μ£ΌμΌ', orientation='h')
fig.show()
fig = px.bar(df, x='μ¬λ§μμ', y='μ¬κ³ μ ν', orientation='h')
fig.show()
# λ°μ΄ν° μ€λΉ
dj_df = df[df['λ°μμ§μλ'] =='λμ ']
fig = px.bar(dj_df, x='μ¬λ§μμ', y='λ°μμκ°', orientation='h')
fig.show()
fig = px.bar(dj_df, x='λ°μμ§μꡰꡬ', y='μ¬λ§μμ')
fig.show()
import folium
dj_map = folium.Map(location=[36.321665,127.378953]) # λμ μμ²μ μλ, κ²½λ κ°
# μ¬λ§μ¬κ³ μμΉμ CircleMarkerνμΆ
# CircleMarker size μ¬λ§μμ+λΆμμμ μ€μ -> ν΄λ¦νλ©΄ νμ
μΌλ‘ μ 보(μ¬κ³ μ ν) λνλλλ‘ κ΅¬ν
# CircleMarker size(radius)λ μ€μν(float64)λ‘λ§ μ€μ κ°λ₯
# μ¬λ§μμ, λΆμμμ int -> float64 λ°μ΄ν° νμ
λ³κ²½
dj_df = dj_df.astype({'μ¬λ§μμ' :'float64'})
dj_df = dj_df.astype({'λΆμμμ' :'float64'})
dj_df.info()
# forλ¬Έμ μ¬μ©νμ¬ dj_df λ°μ΄ν°κ° μμ λκΉμ§ λ°λ³΅μ μΌλ‘ CircleMarker μ§λμ add μν€λ μμ
-> 74λ²
for n in dj_df.index:
# CircleMarker μ¬μ΄μ¦ κ³μ°
cnt = dj_df['μ¬λ§μμ'][n] + dj_df['λΆμμμ'][n]
folium.CircleMarker([dj_df['μλ'][n],dj_df['κ²½λ'][n]], radius =cnt*10, popup=dj_df['μ¬κ³ μ ν'][n],
color='#3186cc', fill_color='#3186cc').add_to(dj_map)
dj_map
β μ μ₯ : map.save('2021_λμ κ΅ν΅μ¬κ³ νν©.html')
1) μκ°λ³ μ¬λ§μ νν© -> colorλ₯Ό μ£Ό,μΌλ‘ νμ
2) μ§μκ΅¬λ³ μ¬λ§μ νν©
3) μ§λλ₯Ό νμ©ν κ΅ν΅μ¬κ³ νν©λΆμ
import pandas as pd
import plotly.express as px
df = pd.read_csv('/content/λλ‘κ΅ν΅κ³΅λ¨_μ¬λ§ κ΅ν΅μ¬κ³ μ 보_20211231.csv', encoding='euc-kr')
df.head()
# μκ°λ§ λ¨κΈ°κΈ°
# 2021-01-01 03:00μμ 03λ§ λ¨κΈ°κΈ°
date_split = df['λ°μλ
μμΌμ'].str.split(' ') # λ μ§μ μκ°:λΆ λΆλ¦¬
time_split = date_split.str.get(1) # μκ°:λΆλ§ μ ν
df['λ°μμκ°'] = time_split.str.split(':').str.get(0) # μκ°λ§ μ ν
df = df.astype({'λ°μμκ°':'int64'})
df.info()
df['λ°μλ
μμΌ'] = pd.to_datetime(date_split.str.get(0), format='%Y-%m-%d', errors='raise')
df.head()
# νμμλ μ»¬λΌ μμ
del df['λ°μλ
μμΌμ']
df.head()
se_df = df[df['λ°μμ§μλ'] =='μμΈ']
se_df.info()
fig = px.bar(se_df, x='μ¬λ§μμ', y='λ°μμκ°', orientation='h', color="μ£ΌμΌ")
fig.show()
fig = px.bar(se_df, x='λ°μμ§μꡰꡬ', y='μ¬λ§μμ')
fig.show()
# forλ¬Έμ μ¬μ©νμ¬ dj_df λ°μ΄ν°κ° μμ λκΉμ§ λ°λ³΅μ μΌλ‘ CircleMarker μ§λμ add μν€λ μμ
-> 74λ²
for n in se_df.index:
# CircleMarker μ¬μ΄μ¦ κ³μ°
cnt1 = se_df['μ¬λ§μμ'][n]
cnt2 = se_df['λΆμμμ'][n]
cnt = cnt1+cnt2
if se_df['μ¬κ³ μ ν'][n] == 'κΈ°ν':
folium.CircleMarker([se_df['μλ'][n],se_df['κ²½λ'][n]], radius =cnt*10, popup='<pre>'+se_df['μ¬κ³ μ ν'][n]+', μ¬λ§μ '+str(int(cnt1))+'λͺ
, '+'λΆμμ '+ str(int(cnt2))+'λͺ
'+'</pre>',
color='red', fill_color='E12727').add_to(se_map)
elif se_df['μ¬κ³ μ ν'][n] == 'ν‘λ¨μ€':
folium.CircleMarker([se_df['μλ'][n],se_df['κ²½λ'][n]], radius =cnt*10, popup='<pre>'+se_df['μ¬κ³ μ ν'][n]+', μ¬λ§μ '+str(int(cnt1))+'λͺ
, '+'λΆμμ '+ str(int(cnt2))+'λͺ
'+'</pre>',
color='blue', fill_color='#3186cc').add_to(se_map)
else:
folium.CircleMarker([se_df['μλ'][n],se_df['κ²½λ'][n]], radius =cnt*10, popup='<pre>'+se_df['μ¬κ³ μ ν'][n]+', μ¬λ§μ '+str(int(cnt1))+'λͺ
, '+'λΆμμ '+ str(int(cnt2))+'λͺ
'+'</pre>',
color='green', fill_color='#cfffbb').add_to(se_map)
se_map
β μ μ₯ : map.save('2021_μμΈνΉλ³μκ΅ν΅μ¬κ³ νν©.html')
β popupμ κ°λ‘ λͺ¨μμΌλ‘ λ°°μΉνκΈ° μν λ°©λ²
νκ°μ λ°μ΄ν°νμΌμ κ°μ§κ³ λ€μν κ·Έλνλ₯Ό 그릴 μ μλ€λ μ μ΄ ν₯λ―Έλ‘μ λ€. μ€λλ μ μ²λ¦¬νλ κ³Όμ μ΄ μ½κ° ^^.. μ΄λ €μ λ€. splitκ³Ό getμ μ΄λλ μ¨μΌνλμ§ κ³μ ν·κ°λ Έμλλ° μ€λ μ μ²λ¦¬νλ κ³Όμ μμ μ€κ°μ κ³μ κ²°κ³Όλ₯Ό λμκ°λ©΄μ νμΈν΄λ³΄λκΉ μ΄ν΄ν μ μμλ€. μμΈνΉλ³μ κ΅ν΅μ¬κ³ νν©μ κ΅μλ λμμμ΄ ν΄λ΄€λλ° κ²°κ³Όκ° μ λμμ μ¬λ―Έμμλ€. μ±κ²©μ΄ λ°λ‘λ°λ‘ ν΄κ²°μ΄ μλλ©΄ λ΅λ΅ν¨μ λλΌκ³ ν¬κΈ°κ°μ μ‘λ νΈμ΄κΈ΄νλ° κ³ μ³μΌκ² μ΄μ© .. .γ γ .....