9 분 소요

import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#df = pd.read_csv('C:\python\project\초혼연령&출산율\평균+초혼연령(구별)_20220927162356.xls', encoding='utf_8')
df = pd.read_excel('C:\python\project\초혼연령&출산율\평균+초혼연령(구별)_20220927162356.xls')
df=df.drop(0,axis=0)
df.set_index('자치구별(1)', inplace=True)
df
2000 2000.1 2001 2001.1 2002 2002.1 2003 2003.1 2004 2004.1 ... 2017 2017.1 2018 2018.1 2019 2019.1 2020 2020.1 2021 2021.1
자치구별(1)
서울시 29.7 27.3 29.9 27.5 30.1 27.8 30.5 28 30.9 28.3 ... 33.4 31.2 33.5 31.3 33.7 31.6 33.6 31.6 33.9 31.9
종로구 29.7 27.8 30.5 27.9 30.7 28.5 31.1 28.7 31.4 28.6 ... 33.1 31.5 33.3 31.3 34.1 31.6 33.7 32.1 33.7 32.1
중구 30.2 27.5 30.3 27.8 30.7 28.1 31.1 28.4 31.8 28.4 ... 33.6 31.5 33.8 31.7 33.9 31.9 33.7 32.1 34.2 32.2
용산구 29.9 27.7 30.3 28.1 30.6 28.2 31 28.4 31.5 28.8 ... 33.7 31.6 33.8 31.9 34.1 32.1 33.9 32.2 34.7 32.8
성동구 29.8 27.2 30 27.4 30.2 27.8 30.7 28 31 28.1 ... 33.5 31.2 33.7 31.6 33.8 31.5 33.8 31.6 34.1 32.2
광진구 29.6 27.2 29.8 27.4 29.9 27.7 30.4 28 30.8 28.3 ... 33.1 31.1 33.4 31.1 33.7 31.5 33.4 31.3 33.4 31.7
동대문구 29.6 27.2 29.8 27.6 30.4 27.7 30.7 28 31.3 28.4 ... 33.7 31.5 33.4 31.3 34 31.7 33.3 31.4 33.6 31.5
중랑구 29.4 26.9 29.6 27.1 30.1 27.4 30.5 27.7 30.6 28 ... 33.6 31.1 33.3 30.9 33.7 31.5 33.6 31.5 33.8 31.6
성북구 29.7 27.2 29.8 27.5 30.1 27.8 30.5 28 31 28.3 ... 33.7 31.2 33.8 31.5 33.9 31.6 33.9 31.8 34 32
강북구 29.3 27 29.8 27.3 30 27.6 30.4 27.8 30.9 28.3 ... 33.5 31 33.7 31 34.2 31.5 34 31.3 34.3 32.1
도봉구 29.6 27.2 29.9 27.6 30.1 27.9 30.5 27.9 30.9 28.3 ... 33.2 30.9 33.5 31 33.6 31.3 33.6 31.6 33.7 31.7
노원구 29.8 27.3 30 27.5 30.2 27.7 30.5 27.9 30.9 28.4 ... 33.1 30.9 33.7 31.2 33.7 31.5 33.4 31.6 33.4 31.5
은평구 29.6 27.3 29.7 27.5 29.9 27.9 30.5 28 30.9 28.4 ... 33.8 31.3 33.7 31.5 33.9 31.4 34 31.6 33.9 32.1
서대문구 29.6 27.6 29.9 27.8 30.3 28 30.6 28.3 31 28.6 ... 33.2 31.2 33.4 31.4 33.6 31.8 33.4 31.8 34 32.1
마포구 29.9 27.5 30 27.8 30.3 28 30.7 28.3 30.9 28.5 ... 33.6 31.5 33.6 31.6 33.8 31.7 34 31.7 34.2 32.1
양천구 29.7 27.1 29.9 27.3 30.2 27.6 30.5 27.7 30.7 28 ... 33.2 31 33.4 31.3 33.7 31.4 33.6 31.4 33.5 31.7
강서구 29.6 27.1 29.8 27.3 30 27.6 30.5 27.8 30.7 28.1 ... 33.2 31.1 33.3 31 33.6 31.4 33.4 31.4 33.7 31.6
구로구 29.5 26.9 29.7 27.3 30 27.4 30.6 27.8 30.8 28.2 ... 33.7 31.3 33.9 31.3 34 31.7 33.7 31.7 33.9 32
금천구 29.5 26.7 29.7 27 30.1 27.5 30.7 27.7 30.9 28 ... 33 30.9 33.3 30.8 33.5 31.4 33.4 31.1 33.9 31.5
영등포구 29.8 27.2 29.8 27.4 30.3 27.8 30.7 28 31.1 28.5 ... 33.2 31 33.1 31.1 33.5 31.4 33.4 31.3 33.7 31.9
동작구 29.6 27.3 30 27.7 30 27.8 30.4 28.2 30.7 28.3 ... 33.2 31.2 33.5 31.4 33.6 31.4 33.6 31.8 33.7 31.7
관악구 29.5 27.2 29.8 27.5 29.9 27.7 30.3 28 30.8 28.2 ... 33 30.8 33.1 31.1 33.3 31.2 33 31.1 33.4 31.5
서초구 29.7 27.7 30.1 28 30.3 28.2 30.5 28.4 30.8 28.6 ... 33.5 31.5 33.7 31.9 33.9 31.8 34 32 34.3 32.5
강남구 29.9 27.7 30.1 27.9 30.3 28.3 30.5 28.4 30.9 28.7 ... 33.9 31.7 33.9 31.9 34.2 32.1 34.1 32.3 34.3 32.6
송파구 29.7 27.3 29.9 27.5 30 27.8 30.2 27.9 30.7 28.2 ... 33.1 31.3 33.4 31.4 33.5 31.6 33.4 31.6 33.9 32.1
강동구 29.6 26.9 29.6 27.2 29.9 27.5 30.3 27.9 30.9 28.2 ... 33.3 31.3 33.5 31.2 33.5 31.4 33.6 31.6 33.8 31.8

26 rows × 44 columns

new_df = pd.DataFrame()
col_li = list(df.columns)
length=int(len(df.columns) / 2)

for i in range(0, length):
    #남녀의 초혼연령의 평균을 계산한다.
    new_df[col_li[i*2]] = (df[col_li[i*2]] + df[col_li[(2*i)+1]])/2
    
new_df = new_df.reset_index()    
new_df=new_df.drop(0,axis=0)
new_df.index = new_df.index-1
new_df
자치구별(1) 2000 2001 2002 2003 2004 2005 2006 2007 2008 ... 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
0 종로구 28.75 29.2 29.6 29.9 30 30.75 30.65 30.65 30.8 ... 31.75 31.9 31.8 32.2 32.4 32.3 32.3 32.85 32.9 32.9
1 중구 28.85 29.05 29.4 29.75 30.1 30.5 30.5 30.35 30.95 ... 31.7 31.7 32 32.55 32.4 32.55 32.75 32.9 32.9 33.2
2 용산구 28.8 29.2 29.4 29.7 30.15 30.6 30.4 30.85 31.15 ... 31.6 32 32.5 32.25 32.4 32.65 32.85 33.1 33.05 33.75
3 성동구 28.5 28.7 29 29.35 29.55 29.95 30.1 30.35 30.7 ... 31.45 31.6 31.9 32.05 32.35 32.35 32.65 32.65 32.7 33.15
4 광진구 28.4 28.6 28.8 29.2 29.55 29.8 29.85 30.15 30.4 ... 31.25 31.35 31.65 31.9 32 32.1 32.25 32.6 32.35 32.55
5 동대문구 28.4 28.7 29.05 29.35 29.85 30 30 30.4 30.55 ... 31.3 31.6 31.75 31.8 32.15 32.6 32.35 32.85 32.35 32.55
6 중랑구 28.15 28.35 28.75 29.1 29.3 29.75 29.9 29.95 30.2 ... 31.3 31.4 31.6 31.65 31.75 32.35 32.1 32.6 32.55 32.7
7 성북구 28.45 28.65 28.95 29.25 29.65 29.95 30.05 30.4 30.55 ... 31.4 31.6 31.7 31.9 32.25 32.45 32.65 32.75 32.85 33
8 강북구 28.15 28.55 28.8 29.1 29.6 29.85 29.95 30.15 30.45 ... 31.2 31.35 31.75 31.8 32.25 32.25 32.35 32.85 32.65 33.2
9 도봉구 28.4 28.75 29 29.2 29.6 29.8 30.05 29.95 30.35 ... 31.15 31.4 31.65 31.7 32.05 32.05 32.25 32.45 32.6 32.7
10 노원구 28.55 28.75 28.95 29.2 29.65 29.75 29.9 30.05 30.5 ... 31.35 31.4 31.85 31.75 32 32 32.45 32.6 32.5 32.45
11 은평구 28.45 28.6 28.9 29.25 29.65 29.95 30.3 30.45 30.6 ... 31.35 31.5 32 32 32.05 32.55 32.6 32.65 32.8 33
12 서대문구 28.6 28.85 29.15 29.45 29.8 30.3 30.2 30.45 30.7 ... 31.55 31.5 31.8 31.85 32.3 32.2 32.4 32.7 32.6 33.05
13 마포구 28.7 28.9 29.15 29.5 29.7 30.05 30.25 30.45 30.75 ... 31.5 31.6 31.95 32.05 32.3 32.55 32.6 32.75 32.85 33.15
14 양천구 28.4 28.6 28.9 29.1 29.35 29.65 29.85 30.05 30.4 ... 31.2 31.25 31.45 31.65 31.9 32.1 32.35 32.55 32.5 32.6
15 강서구 28.35 28.55 28.8 29.15 29.4 29.7 29.85 30.05 30.35 ... 31.25 31.4 31.6 31.8 32.1 32.15 32.15 32.5 32.4 32.65
16 구로구 28.2 28.5 28.7 29.2 29.5 29.75 29.9 30.1 30.45 ... 31.3 31.45 31.65 31.85 32.2 32.5 32.6 32.85 32.7 32.95
17 금천구 28.1 28.35 28.8 29.2 29.45 29.9 29.85 30.25 30.45 ... 31.2 31.25 31.5 31.45 31.85 31.95 32.05 32.45 32.25 32.7
18 영등포구 28.5 28.6 29.05 29.35 29.8 29.95 30 30.25 30.7 ... 31.35 31.45 31.7 31.8 31.95 32.1 32.1 32.45 32.35 32.8
19 동작구 28.45 28.85 28.9 29.3 29.5 29.85 30 30.35 30.55 ... 31.3 31.45 31.7 31.95 32 32.2 32.45 32.5 32.7 32.7
20 관악구 28.35 28.65 28.8 29.15 29.5 29.8 29.9 30.1 30.35 ... 31.1 31.35 31.6 31.7 31.7 31.9 32.1 32.25 32.05 32.45
21 서초구 28.7 29.05 29.25 29.45 29.7 30.05 30.15 30.45 30.75 ... 31.5 31.7 31.95 32 32.35 32.5 32.8 32.85 33 33.4
22 강남구 28.8 29 29.3 29.45 29.8 30.05 30.15 30.5 30.65 ... 31.6 31.7 32.05 32.2 32.65 32.8 32.9 33.15 33.2 33.45
23 송파구 28.5 28.7 28.9 29.05 29.45 29.85 29.8 30.2 30.4 ... 31.25 31.45 31.7 31.85 32.1 32.2 32.4 32.55 32.5 33
24 강동구 28.25 28.4 28.7 29.1 29.55 29.6 29.75 29.95 30.2 ... 31.2 31.35 31.6 31.85 32.15 32.3 32.35 32.45 32.6 32.8

25 rows × 23 columns

df2 = pd.read_excel('C:\python\project\초혼연령&출산율\합계출산율+및+모의+연령별+출산율_20220927204055.xls')
df2=df2.drop([0,1],axis=0)

#1997~1999까지의 자료는 비어있으므로, 열에서 제거해준다.
df2=df2.drop(['1997','1998','1999'],axis=1)
df2=df2.rename(columns={'자치구별(1)':'자치구'})
df2.index = df2.index-2
df2
자치구 2000 2001 2002 2003 2004 2005 2006 2007 2008 ... 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
0 종로구 1.097 0.972 0.869 0.88 0.846 0.778 0.811 0.879 0.857 ... 0.89 0.729 0.79 0.813 0.784 0.646 0.606 0.648 0.522 0.531
1 중구 1.225 1.102 1.018 0.913 0.997 0.903 0.942 1.084 0.978 ... 1.121 0.955 1.004 0.946 0.974 0.824 0.737 0.78 0.688 0.634
2 용산구 1.233 1.105 0.937 0.951 0.989 0.906 0.965 1.081 1.017 ... 1.098 1.008 0.978 1.005 0.907 0.793 0.747 0.713 0.634 0.664
3 성동구 1.342 1.194 1.097 1.1 1.066 0.985 1.035 1.154 1.091 ... 1.077 1.079 1.027 1.095 1.013 0.972 0.909 0.855 0.783 0.764
4 광진구 1.32 1.115 1.012 1.035 0.982 0.881 0.945 0.991 0.939 ... 0.957 0.877 0.895 0.903 0.867 0.749 0.713 0.652 0.527 0.525
5 동대문구 1.181 1.048 0.982 0.923 0.956 0.879 0.939 1.009 0.993 ... 0.998 0.909 0.914 0.96 0.889 0.822 0.753 0.765 0.699 0.66
6 중랑구 1.22 1.045 0.992 0.947 1.008 0.915 0.978 1.046 1.01 ... 1.082 0.991 1.006 1.051 0.995 0.906 0.825 0.777 0.66 0.65
7 성북구 1.186 1.042 0.899 1 1.025 0.946 1.012 1.12 1.042 ... 1.044 0.983 0.949 0.991 0.909 0.818 0.737 0.707 0.676 0.656
8 강북구 1.077 0.992 0.922 0.988 0.921 0.858 0.886 1.032 0.971 ... 1.051 0.963 0.955 0.922 0.878 0.825 0.647 0.617 0.55 0.541
9 도봉구 1.353 1.178 1.061 1.057 1.086 0.964 0.986 1.079 1.015 ... 1.137 0.956 1 1.01 0.947 0.833 0.774 0.697 0.601 0.579
10 노원구 1.524 1.349 1.177 1.216 1.197 1.061 1.114 1.21 1.134 ... 1.162 1.057 1.109 1.096 1.068 0.938 0.809 0.776 0.695 0.701
11 은평구 1.258 1.075 1.012 1.074 1.044 0.949 0.994 1.07 0.966 ... 1.139 1.024 1.028 1.003 0.943 0.814 0.772 0.697 0.623 0.604
12 서대문구 1.206 1.092 1.009 0.999 0.99 0.902 0.92 1.012 0.958 ... 0.967 0.938 0.954 0.913 0.934 0.823 0.727 0.685 0.685 0.635
13 마포구 1.239 1.082 0.986 0.931 1.022 0.901 0.967 1.077 0.997 ... 0.991 0.892 0.915 0.986 0.945 0.806 0.733 0.681 0.594 0.587
14 양천구 1.259 1.149 1.059 1.049 1.022 0.961 1.019 1.124 1.043 ... 1.055 0.99 1.005 1.034 1.008 0.898 0.771 0.742 0.644 0.645
15 강서구 1.404 1.185 1.075 1.102 1.095 1.026 1.064 1.165 1.123 ... 1.151 1.032 1.064 1.129 1.059 0.925 0.826 0.76 0.675 0.617
16 구로구 1.431 1.269 1.142 1.131 1.147 1.082 1.156 1.232 1.202 ... 1.258 1.162 1.142 1.169 1.07 0.943 0.838 0.794 0.737 0.737
17 금천구 1.301 1.129 0.997 1.028 1.063 0.951 1.052 1.08 1.01 ... 1.104 0.944 1.019 1.086 0.944 0.901 0.796 0.709 0.668 0.612
18 영등포구 1.387 1.194 1.085 1.106 1.101 1.05 1.101 1.208 1.116 ... 1.142 0.999 1.025 1.085 1.011 0.884 0.851 0.829 0.705 0.713
19 동작구 1.253 1.156 0.996 1.003 1.057 0.97 1.005 1.061 1.008 ... 1.017 0.974 1.007 1.012 0.912 0.831 0.737 0.697 0.655 0.602
20 관악구 1.273 1.091 0.979 0.993 0.976 0.91 0.922 1.005 0.942 ... 0.891 0.825 0.834 0.833 0.777 0.662 0.597 0.536 0.474 0.437
21 서초구 1.135 0.958 0.901 0.896 0.917 0.843 0.904 0.973 0.972 ... 1.095 1.007 1.014 1.012 0.928 0.799 0.8 0.715 0.656 0.666
22 강남구 1.129 0.926 0.81 0.827 0.792 0.73 0.797 0.877 0.823 ... 0.886 0.842 0.863 0.857 0.802 0.706 0.631 0.612 0.537 0.523
23 송파구 1.341 1.135 1.036 1.034 0.991 0.931 0.958 1.046 0.996 ... 1.099 0.973 0.996 1.008 0.958 0.871 0.8 0.77 0.665 0.623
24 강동구 1.347 1.14 1.06 1.039 1.038 0.958 0.993 1.073 1.008 ... 1.101 1.006 1.039 1.036 0.938 0.884 0.82 0.758 0.748 0.797

25 rows × 23 columns

댓글남기기