import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
%matplotlib inline
font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family=font_name)
os.chdir(r"C:\Users\Gram\Desktop\아시아경제 수업자료\01 Python 분석 기초 - 실습\data")
df = pd.read_csv('data_population.csv', header='infer',encoding = 'ms949')
df.shape
(281, 9)
df
Unnamed: 0 | Province | City | Population | Households | PersInHou | Male | Female | GenderRatio | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 서울특별시 | 10078850 | 4197478 | 2.40 | 4962774 | 5116076 | 0.97 | |
1 | 2 | 서울특별시 | 종로구 | 155695 | 72882 | 2.14 | 76962 | 78733 | 0.98 |
2 | 3 | 서울특별시 | 중구 | 126817 | 59614 | 2.13 | 63292 | 63525 | 1.00 |
3 | 4 | 서울특별시 | 용산구 | 235186 | 108138 | 2.17 | 114119 | 121067 | 0.94 |
4 | 5 | 서울특별시 | 성동구 | 298145 | 126915 | 2.35 | 148265 | 149880 | 0.99 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
276 | 277 | 경상남도 | 거창군 | 63069 | 27724 | 2.27 | 30582 | 32487 | 0.94 |
277 | 278 | 경상남도 | 합천군 | 48856 | 24193 | 2.02 | 23231 | 25625 | 0.91 |
278 | 279 | 제주특별자치도 | 615250 | 251478 | 2.45 | 308627 | 306623 | 1.01 | |
279 | 280 | 제주특별자치도 | 제주시 | 454673 | 182709 | 2.49 | 227549 | 227124 | 1.00 |
280 | 281 | 제주특별자치도 | 서귀포시 | 160577 | 68769 | 2.34 | 81078 | 79499 | 1.02 |
281 rows × 9 columns
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 281 entries, 0 to 280 Data columns (total 9 columns): Unnamed: 0 281 non-null int64 Province 281 non-null object City 281 non-null object Population 281 non-null int64 Households 281 non-null int64 PersInHou 281 non-null float64 Male 281 non-null int64 Female 281 non-null int64 GenderRatio 281 non-null float64 dtypes: float64(2), int64(5), object(2) memory usage: 19.9+ KB
df.head(10)
Unnamed: 0 | Province | City | Population | Households | PersInHou | Male | Female | GenderRatio | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 서울특별시 | 10078850 | 4197478 | 2.40 | 4962774 | 5116076 | 0.97 | |
1 | 2 | 서울특별시 | 종로구 | 155695 | 72882 | 2.14 | 76962 | 78733 | 0.98 |
2 | 3 | 서울특별시 | 중구 | 126817 | 59614 | 2.13 | 63292 | 63525 | 1.00 |
3 | 4 | 서울특별시 | 용산구 | 235186 | 108138 | 2.17 | 114119 | 121067 | 0.94 |
4 | 5 | 서울특별시 | 성동구 | 298145 | 126915 | 2.35 | 148265 | 149880 | 0.99 |
5 | 6 | 서울특별시 | 광진구 | 362197 | 158769 | 2.28 | 177946 | 184251 | 0.97 |
6 | 7 | 서울특별시 | 동대문구 | 362604 | 160110 | 2.26 | 181825 | 180779 | 1.01 |
7 | 8 | 서울특별시 | 중랑구 | 417976 | 177077 | 2.36 | 208657 | 209319 | 1.00 |
8 | 9 | 서울특별시 | 성북구 | 464176 | 192670 | 2.41 | 227676 | 236500 | 0.96 |
9 | 10 | 서울특별시 | 강북구 | 333523 | 141325 | 2.36 | 164304 | 169219 | 0.97 |
df.tail(10)
Unnamed: 0 | Province | City | Population | Households | PersInHou | Male | Female | GenderRatio | |
---|---|---|---|---|---|---|---|---|---|
271 | 272 | 경상남도 | 고성군 | 55563 | 25870 | 2.15 | 27848 | 27715 | 1.00 |
272 | 273 | 경상남도 | 남해군 | 46238 | 22259 | 2.08 | 22105 | 24133 | 0.92 |
273 | 274 | 경상남도 | 하동군 | 50068 | 23392 | 2.14 | 24713 | 25355 | 0.97 |
274 | 275 | 경상남도 | 산청군 | 36122 | 17985 | 2.01 | 17630 | 18492 | 0.95 |
275 | 276 | 경상남도 | 함양군 | 40314 | 19229 | 2.10 | 19259 | 21055 | 0.91 |
276 | 277 | 경상남도 | 거창군 | 63069 | 27724 | 2.27 | 30582 | 32487 | 0.94 |
277 | 278 | 경상남도 | 합천군 | 48856 | 24193 | 2.02 | 23231 | 25625 | 0.91 |
278 | 279 | 제주특별자치도 | 615250 | 251478 | 2.45 | 308627 | 306623 | 1.01 | |
279 | 280 | 제주특별자치도 | 제주시 | 454673 | 182709 | 2.49 | 227549 | 227124 | 1.00 |
280 | 281 | 제주특별자치도 | 서귀포시 | 160577 | 68769 | 2.34 | 81078 | 79499 | 1.02 |
header = df.columns
header
Index(['Unnamed: 0', 'Province', 'City', 'Population', 'Households', 'PersInHou', 'Male', 'Female', 'GenderRatio'], dtype='object')
충청남도와 충청북도의 전체 인구 집계:
df2 = df[df.Province=='충청북도']
df2.Population.sum()
3996749
df3 = df[df.Province=='충청남도']
df3.Population.sum()
4739827
df4 = df[(df.Province=='충청북도') | (df.Province=='충청남도')]
df4.Population.sum()
8736576
성비 기준으로 남자가 많고 (GenderRatio >1) 1인 가구가 많은 (PersInHou<2) 도시:
df5 = df[(df.GenderRatio > 1) & (df.PersInHou < 2)]
my_labels = list(df5.City)
my_GenderRatio = list(df5.GenderRatio)
df6 = pd.DataFrame( {'GenderRatio':my_GenderRatio}, index = my_labels)
df6
GenderRatio | |
---|---|
옹진군 | 1.29 |
화천군 | 1.27 |
고성군 | 1.15 |
괴산군 | 1.03 |
신안군 | 1.11 |
울릉군 | 1.15 |
df6.plot.bar(color='orange', alpha=0.7)
plt.show()
df7 = df6.sort_values(by='GenderRatio', ascending=False)
df7.plot.bar(color='green', alpha=0.8) #alpa는 투명도
plt.show()
df7 = df6.sort_values(by='GenderRatio', ascending=True)
df7.plot.bar(color='blue', alpha=0.8)
plt.show()