카테고리 없음
19. 출생아 수 및 합계 출산율
_션샤인
2024. 7. 5. 17:03
import pandas as pd
df = pd.read_excel('142801_20240410181128997_excel.xlsx', skiprows = 2, nrows = 2, index_col = 0)
df
df.index.values
array(['출생아\xa0수', '합계\xa0출산율'], dtype=object)
df.index
Index(['출생아 수', '합계 출산율'], dtype='object')
df.rename(index={'출생아\xa0수' : '출생아 수', '합계\xa0출산율': '합계 출산율'}, inplace = True)
df
df.index.values
array(['출생아 수', '합계 출산율'], dtype=object)
df.loc['출생아 수']
2011 471.3
2012 484.6
2013 436.5
2014 435.4
2015 438.4
2016 406.2
2017 357.8
2018 326.8
2019 302.7
2020 272.3
Name: 출생아 수, dtype: float64
df.iloc[0]
2011 471.3
2012 484.6
2013 436.5
2014 435.4
2015 438.4
2016 406.2
2017 357.8
2018 326.8
2019 302.7
2020 272.3
Name: 출생아 수, dtype: float64
df = df.T
df
데이터 시각화
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = 'Malgun Gothic'
matplotlib.rcParams['font.size'] = 15
matplotlib.rcParams['axes.unicode_minus'] = False
plt.plot(df.index, df['출생아 수'])
plt.plot(df.index, df['합계 출산율'])
fig, ax1 = plt.subplots()
ax1.plot(df.index, df['출생아 수'], color='#ff811d')
ax2 = ax1.twinx() #x축을 공유하는 쌍둥이 axis
ax2.plot(df.index, df['합계 출산율'], color='#ffd100')
fig, ax1 = plt.subplots(figsize = (13,5))
ax1.set_ylabel('출생아 수 (천 명)')
ax1.set_ylim(250, 700)
ax1.set_yticks([300, 400, 500, 600])
ax1.bar(df.index, df['출생아 수'], color='#ff811d')
ax2 = ax1.twinx() #x축을 공유하는 쌍둥이 axis
ax2.set_ylabel('합계 출산율 (가임여성 1명당 명)')
ax2.set_ylim(0, 1.5)
ax2.set_yticks([0,1])
ax2.plot(df.index, df['합계 출산율'], color='#ffd100', linewidth = 3)
fig, ax1 = plt.subplots(figsize = (13,5))
fig.suptitle('출생아 수 및 합계출산율')
ax1.set_ylabel('출생아 수 (천 명)')
ax1.set_ylim(250, 700)
ax1.set_yticks([300, 400, 500, 600])
ax1.grid(axis = 'y', color = 'grey', alpha = 0.2)
ax1.bar(df.index, df['출생아 수'], color='#ff811d')
for idx, val in enumerate(df['출생아 수']):
ax1.text(idx, val +15, val, ha = 'center')
ax2 = ax1.twinx() #x축을 공유하는 쌍둥이 axis
ax2.set_ylabel('합계 출산율 (가임여성 1명당 명)')
ax2.set_ylim(0, 1.5)
ax2.set_yticks([0,1])
ax2.plot(df.index, df['합계 출산율'], color='#ffd100', lw = 3, marker = 'o', mec = 'white', ms = 10, mew = 3)
for idx, val in enumerate(df['합계 출산율']):
ax2.text(idx, val + 0.05, val, ha = 'center')