File name
Commit message
Commit date
File name
Commit message
Commit date
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "fec63b3f-eb21-40d6-a807-c270fc17275d",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4a399c5-d032-447d-9a5a-32fcef23fbcd",
"metadata": {},
"outputs": [],
"source": [
"cctvdata = pd.read_csv('seoul_cctv_202112.csv', encoding='cp949', skiprows=1, index_col='구분', thousands=',')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bc74eccf-fd7e-41f2-b230-d0fa05c03ceb",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3971fdf8-c8ed-49c5-9814-ae70399632db",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.drop('계', inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c99dc712-7607-4f50-a884-1eedb1af8deb",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dd5215b-cf27-4fa2-b6a4-057354cb4981",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.reset_index(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f56b5283-5eb6-42c4-b77f-918cf64ab218",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70df1a3f-2390-41f7-86d6-ffad481455cc",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.rename(columns={'구분':'구별'}, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa20ad9a-faee-4b83-a544-666e95af9e26",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f2bb5c0-7f0a-41fa-acb7-be01d36aeef8",
"metadata": {},
"outputs": [],
"source": [
"popdata = pd.read_excel('seoul_population.xls', header=2, usecols='B, D, G, J, N')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dbaa6931-3a4e-4cbd-9549-8e586c919c2f",
"metadata": {},
"outputs": [],
"source": [
"popdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3fbf8d68-0787-4d33-9b4b-49fec65eaaad",
"metadata": {},
"outputs": [],
"source": [
"popdata.rename(columns={'자치구':'구별',\n",
" '계':'인구수',\n",
" '계.1':'한국인',\n",
" '계.2':'외국인',\n",
" '65세이상고령자':'고령자'}, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97530269-056e-4ef9-88e6-357e7c0d9ef6",
"metadata": {},
"outputs": [],
"source": [
"popdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca429d15-1806-4357-9845-dbc61827ef3b",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.sort_values(by='총계', ascending=False).head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ebca079a-fbf9-4dbd-912e-a2cef7f6022f",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.isnull()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "93420311-e679-4a0a-a439-a72bc3a9e679",
"metadata": {},
"outputs": [],
"source": [
"# 결측치 처리 : 0으로 치환\n",
"cctvdata = cctvdata.fillna(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d095117c-c01d-4ec5-b612-59cba46abe3d",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c881dfa5-19ba-40cc-a689-74c5ec5eee01",
"metadata": {},
"outputs": [],
"source": [
"# str to int\n",
"for col in cctvdata.columns:\n",
" if col=='구별':\n",
" continue\n",
" else:\n",
" cctvdata[col] = pd.to_numeric(cctvdata[col])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e691bc55-8b03-44fc-b7f3-316f5c4c3736",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c07d9499-a67f-47c9-b979-95de42539a36",
"metadata": {},
"outputs": [],
"source": [
"cctvdata['최근증가율'] = (cctvdata['2021년']+cctvdata['2020년']+cctvdata['2019년']+cctvdata['2018년']+cctvdata['2017년'])*100/(cctvdata['2016년']+cctvdata['2015년']+cctvdata['2014년']+cctvdata['2013년']+cctvdata['2012년']+cctvdata['2012년 이전'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad2b0de9-7b4f-43be-baa7-05a4c2850a1f",
"metadata": {},
"outputs": [],
"source": [
"cctvdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f9a5203-2ef2-40ae-b82e-bf4e3cf5576c",
"metadata": {},
"outputs": [],
"source": [
"popdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df89ddcd-6605-4c31-8a3f-7c0be71ae931",
"metadata": {},
"outputs": [],
"source": [
"popdata.drop(0, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aae35dc3-378d-4a7c-b1d1-6b3b4f118896",
"metadata": {},
"outputs": [],
"source": [
"popdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecad3014-faab-4ea0-8baa-33064f576311",
"metadata": {},
"outputs": [],
"source": [
"popdata.reset_index(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7266b86f-21d4-4e15-8cf4-0becc3caf300",
"metadata": {},
"outputs": [],
"source": [
"popdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d0a4c48-f203-41ba-a37b-ca740929e875",
"metadata": {},
"outputs": [],
"source": [
"del popdata['index']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16504402-add5-493b-b613-6a7dff1ffe76",
"metadata": {},
"outputs": [],
"source": [
"popdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31efa200-fa9e-4aa6-aeca-5087e59ce1d4",
"metadata": {},
"outputs": [],
"source": [
"popdata['외국인비율'] = popdata['외국인'] / popdata['인구수'] * 100\n",
"popdata['고령자비율'] = popdata['고령자'] / popdata['인구수'] * 100"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ddf9a5a-d6c4-48b8-b0c2-eb1d805b337b",
"metadata": {},
"outputs": [],
"source": [
"popdata.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a81fa14-f6d8-400e-81a0-fe82151135e0",
"metadata": {},
"outputs": [],
"source": [
"popdata.sort_values(by='인구수', ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "715da3d8-1c72-46d1-b5ed-8e2edd7a644d",
"metadata": {},
"outputs": [],
"source": [
"popdata.sort_values(by='고령자', ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a09e0777-9db8-44c3-ae46-5e493a77f3a1",
"metadata": {},
"outputs": [],
"source": [
"popdata.sort_values(by='고령자비율', ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fa82489-56c5-4b1e-b649-3f66824424f5",
"metadata": {},
"outputs": [],
"source": [
"popdata.sort_values(by='외국인비율', ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5850abc-83de-4730-af10-934257d2ef0a",
"metadata": {},
"outputs": [],
"source": [
"dataset = pd.merge(cctvdata, popdata, on='구별')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c87d68b-8db7-48e2-b682-ed4f2ac075a3",
"metadata": {},
"outputs": [],
"source": [
"dataset.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f61e4c23-714c-4063-b9a3-1b93b5e2fd01",
"metadata": {},
"outputs": [],
"source": [
"del dataset['2012년 이전']\n",
"del dataset['2012년']\n",
"del dataset['2013년']\n",
"del dataset['2014년']\n",
"del dataset['2015년']\n",
"del dataset['2016년']\n",
"del dataset['2017년']\n",
"del dataset['2018년']\n",
"del dataset['2019년']\n",
"del dataset['2020년']\n",
"del dataset['2021년']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b691df20-6b18-49ea-bf10-1f2115f6838e",
"metadata": {},
"outputs": [],
"source": [
"dataset.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c0e8211a-a1aa-4bab-8b05-d8edfa6a99bf",
"metadata": {},
"outputs": [],
"source": [
"dataset.set_index('구별', inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "122789c2-3c24-4c24-a4b5-76b86cee7049",
"metadata": {},
"outputs": [],
"source": [
"dataset.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c1e9fbaa-0e12-4cbb-8575-b025a9e148d9",
"metadata": {},
"outputs": [],
"source": [
"np.corrcoef(dataset['고령자비율'], dataset['총계'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "60d499df-ecf3-4e31-bdac-afb1a3d053bb",
"metadata": {},
"outputs": [],
"source": [
"np.corrcoef(dataset['외국인비율'], dataset['총계'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52e88ee7-58a6-4265-97d4-21971b8efb0c",
"metadata": {},
"outputs": [],
"source": [
"np.corrcoef(dataset['인구수'], dataset['총계'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27605558-2948-4bcb-8c58-f982726c2449",
"metadata": {},
"outputs": [],
"source": [
"dataset.sort_values(by='총계', ascending=False).head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c008ad5-f012-48ce-8dec-cec36af57aee",
"metadata": {},
"outputs": [],
"source": [
"dataset.sort_values(by='인구수', ascending=False).head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "376b9ec4-313e-4ef8-9f1b-6581dd85bcdb",
"metadata": {},
"outputs": [],
"source": [
"plt.rc('font', family='NanumGothic')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bdab3f2-d0e7-459a-9d2b-d254b97af2c3",
"metadata": {},
"outputs": [],
"source": [
"dataset['총계'].plot(kind='barh', grid=True, figsize=(10, 10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "958c14ef-f009-4771-82ac-352563801055",
"metadata": {},
"outputs": [],
"source": [
"dataset['총계'].sort_values().plot(kind='barh', grid=True, figsize=(10, 10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0affbf15-4bde-47d0-85f3-52e10a58d09b",
"metadata": {},
"outputs": [],
"source": [
"dataset.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e340e4a-8307-4c6e-b327-ace8b01b85c0",
"metadata": {},
"outputs": [],
"source": [
"dataset['CCTV비율'] = dataset['총계'] / dataset['인구수'] * 100"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5f16688-4554-44f9-a70f-a55c61f6e34b",
"metadata": {},
"outputs": [],
"source": [
"dataset.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}