-
외국인 입국 통계 데이터 분석 With PythonData Analysis/Social Issues 2021. 4. 14. 21:22
1. 데이터 가져오기
국가 통계 포털에서 2018년부터 2020년까지 월별 국적, 목적별 입구 데이터를 가져와서 분석하도록 하겠다. 코드에 문제가 없다면 아래와 같은 데이터를 확인할 수 있을 것이다.
import pandas as pd df=pd.read_csv('C:/Users/banad/Downloads/입국.csv',encoding='cp949') df.columns=['Continent','Country','Purpose','Month','Visitors'] df['Year']=df['Month'].apply(lambda x:x.split('. ')[0]) df['Purpose']=df['Purpose'].str.replace('관광','Tour').replace('상용','Business').replace('공용','Public').replace('유학연수','Study').replace('기타','etc') df['Date']=df['Month'].apply(lambda x:x.split('. ')[0] +'-'+ x.split('. ')[-1])
2. 2018년~2021년 목적별 입국 수 시각화
너무나도 뻔한 결과가 나타났다. 20년 코로나가 터지기 난 이후부터 관광, 유학, 공용, 상용 전반적인 입국 수가 모두 감소하였다.
import plotly.express as px from datetime import datetime as dt total=df[df['Continent']=='총계'] fig = px.line(total, x="Date", y="Visitors", color='Purpose',title='Number of Visitors by purpose ') fig.show() grp=fig.to_json()
2. 2018년~2021년 대륙별 입국 수 시각화
다음으로 연도 기준으로 대륙별 입국 수 비중을 시각화하도록 하겠다.
20년까지는 대륙별 비중에 크게 차이는 나타나지 않았다. 다만 물론 1개월에 불과하기는 하지만 20년 1월에는 아시아 입국자 수 비중이 크게 감소된 점을 확인할 수 있다.
import plotly.graph_objects as go from plotly.subplots import make_subplots df['Continent']=df['Continent'].str.replace( '총계','Total').replace('아시아주','Asia').replace('미주', 'America').replace('구주', 'Europe').replace( '대양주','Oceania').replace('기타','etc').replace('교포','Foreign_korean') df2=df[df['Continent']!='총계'] df3=df2[df2['Country']!='소계'] df4=pd.DataFrame(df3.groupby(['Continent','Year']).sum()['Visitors']).reset_index() year=pd.DataFrame(total.groupby(['Year']).sum()['Visitors']).reset_index() year2=pd.merge(left=df4,right=year,how='left',left_on=df4['Year'],right_on= year['Year']) year2['shares']=year2['Visitors_x']/year2['Visitors_y'] # Create subplots: use 'domain' type for Pie subplot fig = make_subplots(rows=1, cols=4, specs=[[{'type':'domain'}, {'type':'domain'},{'type':'domain'},{'type':'domain'}]]) fig.add_trace(go.Pie(labels=year2[year2['Year_x']=='2018']['Continent'].values, values=year2[year2['Year_x']=='2018']['shares'].values, name="2018"), 1, 1) fig.add_trace(go.Pie(labels=year2[year2['Year_x']=='2019']['Continent'].values, values=year2[year2['Year_x']=='2019']['shares'].values, name="2019"), 1, 2) fig.add_trace(go.Pie(labels=year2[year2['Year_x']=='2020']['Continent'].values, values=year2[year2['Year_x']=='2020']['shares'], name="2020"), 1, 3) fig.add_trace(go.Pie(labels=year2[year2['Year_x']=='2021']['Continent'].values, values=year2[year2['Year_x']=='2021']['shares'], name="2021"), 1, 4) # Use `hole` to create a donut-like pie chart fig.update_traces(hole=.4, hoverinfo="label+percent+name") fig.update_layout( title_text="Number of Visitors by Continents 2018~2021", # Add annotations in the center of the donut pies. annotations=[dict(text='2018', x=0.08, y=0.5, font_size=15, showarrow=False), dict(text='2019', x=0.37, y=0.5, font_size=15, showarrow=False), dict(text='2020', x=0.63, y=0.5, font_size=15, showarrow=False), dict(text='2021', x=0.925, y=0.5, font_size=15, showarrow=False), ] ) fig.show()
3. 2018년~2021년 연도/대륙/목적별 입국 수 시각화 TreeMap
이번에는 Treemap이라는 좀 색다른 시각화를 통해서 연도, 대륙 그리고 목적 기준을 한번에 시각화 해보도록 하겠다. 기존 전형적인 그래프 대비해서는 그래도 한 눈에 데이터가 들어오지 않나 생각한다.
대륙별, 연도별 모두 봐도 확실히 관광 목적으로 입국하는 수가 가장 높게 나타났다.
import plotly.express as px yy=pd.DataFrame(df.groupby(['Year','Continent','Country','Purpose']).sum()['Visitors']).reset_index() yy=yy[yy['Continent']!='총계'] yy=yy[yy['Country']!='소계'] fig = px.treemap(yy, path=['Year','Continent', 'Country'], values='Visitors',) fig.show()
'Data Analysis > Social Issues' 카테고리의 다른 글
국내 목욕탕/사우나 폐업 수 데이터 분석 & 시각화 With Python (0) 2021.08.22 코로나19 예방접종센터 현황 확인 - 지도 시각화 With Python, Pydeck (1) 2021.06.09 2010~2020년 국내 시도별 예금은행 예금액 분석 with Python (0) 2021.04.13 전국 시도별 빈집 상황 알아보자 with Python (0) 2021.04.11 2015년 ~ 2019년 대한민국 인구 10만명당 자살 통계 데이터 분석 With Python (0) 2021.04.02