교통데이터 분석

찌니·2022년 1월 16일
1
post-thumbnail

인천교통공사 인턴 < 데이터 분석 과제 >


  • 리뷰
  1. 지역별 통근/통학 데이터 분석

    1) 분석 결과 요약
    인천광역시 통근/통학 분석
    연령별 10~75세 이상, 행정구역별
    남녀의 각 통근/통학 비교

  • 2020년 남자 917,146명, 여자 696,416명
    인천광역시의 지하철 이용
  • 2020년 남녀 전체 통근/통학 평균 인구
    연령별 분류 시 남자의 이용률이 더 높음

(1). 통학/통근 데이터

# 선언
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# 시각화 했을 때 한글 나오게
from matplotlib import font_manager, rc
font_name = font_manager.FontProperties(fname="c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family=font_name)

# 파일 불러옴
file_path = 'C:/Users/User/Desktop/workspace/성별_연령별_통근_통학_유형별_인구.csv'
boarding = pd.read_csv(file_path, encoding = 'cp949')
boarding
# 인덱스 0번째 삭제
boarding_rs = boarding.drop(boarding.index[0])
boarding_rs
# boarding_rs 최종
boarding_rs.rename(columns = {'2020':'계'}, inplace = True)
boarding_rs
boarding_rs['계']=boarding_rs['계'].astype(int)
boarding_rs.info()
>> <class 'pandas.core.frame.DataFrame'>
Int64Index: 308 entries, 1 to 308
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype 
---  ------      --------------  ----- 
 0   행정구역별(시군구)  308 non-null    object
 1   성별          308 non-null    object
 2   연령별         308 non-null    object
 3   계           308 non-null    int32 
dtypes: int32(1), object(3)
memory usage: 10.8+ KB

# 인천광역시 전체
total_rs= boarding_rs
[(boarding_rs['행정구역별(시군구)'] == '인천광역시')]
total_rs

# barplot으로 여러 성별을 한눈에 볼 수 있게 -> 전체
sns.barplot(x='성별', y = '계',hue = '연령별', data=total_rs, palette='BuGn_r', alpha=0.7)
plt.show() 

# 남여 전체 평균소득의 평균
plt.title('연령별 평균 통학')
total_rs.groupby('연령별')['계'].mean().plot(kind='bar')
plt.xticks(rotation=45)
>>(array([ 0,  1,  2,  3,  4,  5,  
6,  7,  8,  9, 10, 11, 12, 13]),
 <a list of 14 Text major ticklabel objects>)

# 인천광역시 전체 total_rs
# 연령대 별로 구분
  12~14세, 15~19세 : 10대
  20~24세, 25~29세 : 20대
  30~34세, 35~39세 : 30대
  40~44세, 45~49세 : 40대
  50~54세, 55~59세 : 50대
  60~64세, 65~69세 : 60대
  70~74세 : 70대
  75세 이상

# 인천광역시 남자  
m_rs= total_rs[(total_rs['행정구역별(시군구)'] == '인천광역시') & (total_rs['성별'] == '남자')]       
m_rs

# 인천광역시 남자 m_rs
# 연령대 별로 구분
m_rs.loc[(m_rs['연령별'] == '12~14세') | (m_rs['연령별'] == '15~19세') , '연령별'] = '10대'
m_rs.loc[(m_rs['연령별'] == '20~24세') | (m_rs['연령별'] == '25~29세') , '연령별'] = '20대'
m_rs.loc[(m_rs['연령별'] == '30~34세') | (m_rs['연령별'] == '35~39세') , '연령별'] = '30대'
m_rs.loc[(m_rs['연령별'] == '40~44세') | (m_rs['연령별'] == '45~49세') , '연령별'] = '40대'
m_rs.loc[(m_rs['연령별'] == '50~54세') | (m_rs['연령별'] == '55~59세') , '연령별'] = '50대'
m_rs.loc[(m_rs['연령별'] == '60~64세') | (m_rs['연령별'] == '65~69세') , '연령별'] = '60대'
m_rs.loc[(m_rs['연령별'] == '70~74세'),'연령별'] = '70대'

# 인천광역시 남자 연령별 통근 평균
m_age = m_rs.groupby(['연령별']).mean()
m_age['계']=m_age['계'].astype(int)
m_age.info()
m_age

# 그래프
m_age.plot()
plt.title("인천광역시 연령별 남자 통근 평균")
# 인천광역시 여자 
f_rs= total_rs[(total_rs['행정구역별(시군구)'] == '인천광역시') & (total_rs['성별'] == '여자')]       
f_rs

# 인천광역시 여자 f_rs
# 연령대 별로 구분
f_rs.loc[(f_rs['연령별'] == '12~14세') | (f_rs['연령별'] == '15~19세') , '연령별'] = '10대'
f_rs.loc[(f_rs['연령별'] == '20~24세') | (f_rs['연령별'] == '25~29세') , '연령별'] = '20대'
f_rs.loc[(f_rs['연령별'] == '30~34세') | (f_rs['연령별'] == '35~39세') , '연령별'] = '30대'
f_rs.loc[(f_rs['연령별'] == '40~44세') | (f_rs['연령별'] == '45~49세') , '연령별'] = '40대'
f_rs.loc[(f_rs['연령별'] == '50~54세') | (f_rs['연령별'] == '55~59세') , '연령별'] = '50대'
f_rs.loc[(f_rs['연령별'] == '60~64세') | (f_rs['연령별'] == '65~69세') , '연령별'] = '60대'
f_rs.loc[(f_rs['연령별'] == '70~74세'),'연령별'] = '70대'

# 인천광역시 여자 통근 평균 
f_age = f_rs.groupby(['연령별']).mean()
f_age['계']=f_age['계'].astype(int)
f_age.info()
f_age
# 그래프
f_age.plot()
plt.title("인천광역시 연령별 여자 통근 평균")

result1 = 
pd.concat([f_age_rs,m_age_rs],axis = 1).astype(int)
print(result1)

# 시군구 확인하기
set(boarding_rs['행정구역별(시군구)'])
>> {'강화군', '계양구', '남동구', '동구', '미추홀구', '부평구', '서구', '연수구', '옹진군', '인천광역시', '중구'}

#'남동구'
data_nd= boarding_rs[(boarding_rs['행정구역별(시군구)'] == '남동구')]
data_nd

# 연령대 별로 구분
data_nd.loc[(data_nd['연령별'] == '12~14세') | (data_nd['연령별'] == '15~19세') , '연령별'] = '10대'
data_nd.loc[(data_nd['연령별'] == '20~24세') | (data_nd['연령별'] == '25~29세') , '연령별'] = '20대'
data_nd.loc[(data_nd['연령별'] == '30~34세') | (data_nd['연령별'] == '35~39세') , '연령별'] = '30대'
data_nd.loc[(data_nd['연령별'] == '40~44세') | (data_nd['연령별'] == '45~49세') , '연령별'] = '40대'
data_nd.loc[(data_nd['연령별'] == '50~54세') | (data_nd['연령별'] == '55~59세') , '연령별'] = '50대'
data_nd.loc[(data_nd['연령별'] == '60~64세') | (data_nd['연령별'] == '65~69세') , '연령별'] = '60대'
data_nd.loc[(data_nd['연령별'] == '70~74세'),'연령별'] = '70대'

# 평균 통학
plt.title('연령별 평균 통학')
data_nd.groupby('연령별')['계'].mean().plot(kind='bar')
plt.xticks(rotation=45)
# barplot으로 여러 성별을 한눈에 볼 수 있게 -> 전체
sns.barplot(x='성별', y = '계',hue = '연령별', data=data_nd, palette='BuGn', alpha=0.7 )
plt.show()
# 남동구 여자 / 남자 통근 평균
result = pd.concat([m_age,f_age],axis = 1)
print(result)
result[['남자 평균','여자 평균']].plot(marker='o')
plt.title('남동구 연령별 통근/통학 평균')

# '서구'
data_sg= boarding_rs[(boarding_rs['행정구역별(시군구)'] == '서구')]
data_sg

# 서구
data_sg.loc[(data_sg['연령별'] == '12~14세') | (data_sg['연령별'] == '15~19세') , '연령별'] = '10대'
data_sg.loc[(data_sg['연령별'] == '20~24세') | (data_sg['연령별'] == '25~29세') , '연령별'] = '20대'
data_sg.loc[(data_sg['연령별'] == '30~34세') | (data_sg['연령별'] == '35~39세') , '연령별'] = '30대'
data_sg.loc[(data_sg['연령별'] == '40~44세') | (data_sg['연령별'] == '45~49세') , '연령별'] = '40대'
data_sg.loc[(data_sg['연령별'] == '50~54세') | (data_sg['연령별'] == '55~59세') , '연령별'] = '50대'
data_sg.loc[(data_sg['연령별'] == '60~64세') | (data_sg['연령별'] == '65~69세') , '연령별'] = '60대'
data_sg.loc[(data_sg['연령별'] == '70~74세'),'연령별'] = '70대'

# 남여 전체 평균소득의 평균
data_sg.groupby('연령별')['계'].mean().plot(kind='bar')
plt.xticks(rotation=45)

# 서구 남자 / 여자 통학 평균
sg_result = pd.concat([msg_age,fsg_age],axis = 1)
print(sg_result)
sg_result[['남자 평균','여자 평균']].plot(marker='o')
plt.title('서구 연령별 통근/통학 평균')

# barplot으로 여러 성별을 한눈에 볼 수 있게 -> 전체
sns.barplot(x='성별', y = '계',hue = '연령별', data=data_sg, palette='BuGn', alpha=0.7 )
plt.show()

(2). 소득 데이터

# 선언
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 소득 분석
# 2020년 6월 기준
file_path = '../binder/소득2006.csv'
data = pd.read_csv(file_path, encoding = 'cp949'
                   , header = 0
                   , usecols = ["시군구명","성별","연령별","평균소득"])
data
# 시군구명 종류 알기
set(data['시군구명'])
>> {'강화군', '계양구', '남동구', '동구', '미추홀구',
'부평구', '서구', '연수구', '옹진군', '전체', '중구'}

-- 데이터 전처리 과정
# na 데이터 삭제
data_rs = data.dropna()
data_rs
# 성별이 0이 아닌 것만 (0 = "전체")
data_gs = data_rs[data_rs['성별'] != 0]
data_gs
# 연령 전체 아니것만
data_fs = data_gs[data_gs['연령별'] != 0]
data_fs
# 시군구명도 전체 빼기
data_fss = data_fs[data_fs ['시군구명'] != '전체']
data_fss

  • 상대적으로 소득이 적은 곳
set(data_fss['시군구명'])
>> {'강화군', '계양구', '남동구', '동구', '미추홀구', '부평구', '서구', '연수구', '옹진군', '중구'}

# 동구
data_dgg= data_fss[(data_fss['시군구명'] == '동구')]
data_dgg

# 동구) 성별 1 = 남성, 2 = 여성
data_dgg.loc[(data_dgg['성별'] == 1) , '성별'] = '남성'
data_dgg.loc[(data_dgg['성별'] == 2) , '성별'] = '여성'
data_dgg

#동구 남자 소득을 초년/중년/노년으로 나눠서 구분
#초년) 15, 20, 30
#중년) 40, 50, 60
#노년) 70, 80

data_dgg.loc[(data_dgg['연령별'] == 15) | (data_dgg['연령별'] == 20) | (data_dgg['연령별'] == 30), '연령별'] = '초년'
data_dgg.loc[(data_dgg['연령별'] == 40) | (data_dgg['연령별'] == 50) | (data_dgg['연령별'] == 60), '연령별'] = '중년'
data_dgg.loc[(data_dgg['연령별'] == 70) | (data_dgg['연령별'] == 80), '연령별'] = '노년'
data_dgg['평균소득']=data_dgg['평균소득'].astype(int)
data_dgg

- 시각화 (그래프)
# hue 비교하고 싶은 것
sns.barplot(x='성별', y = '평균소득',hue = '연령별', 
data=data_dgg, palette='BuGn', alpha=0.7 )
plt.title('동구 연령별 평균 소득 비교')
plt.show()

sns.relplot(x = '연령별', y = '평균소득',
hue ='성별', palette='Pastel2',data=data_dgg)
plt.show()

  • 소득이 상대적으로 많은 곳
# '서구',
data_sg= data_fss[(data_fss['시군구명'] == '서구')]
data_sg

# 전체 빈도수
data_sg['평균소득'].plot(kind = 'hist')
- 서구
# 연령을 초년 중년 노년으로 나누기
data_sg.loc[(data_sg['연령별'] == 15) | (data_sg['연령별'] == 20) | (data_sg['연령별'] == 30), '연령별'] = '초년'
data_sg.loc[(data_sg['연령별'] == 40) | (data_sg['연령별'] == 50) | (data_sg['연령별'] == 60), '연령별'] = '중년'
data_sg.loc[(data_sg['연령별'] == 70) | (data_sg['연령별'] == 80), '연령별'] = '노년'
print(data_sg)
set(data_sg['시군구명'])

>>      시군구명  성별 연령별   평균소득
6680    서구  남성  초년   2321
6681    서구  여성  중년   1746
6682    서구  여성  중년   2563
6683    서구  여성  초년   3444
6684    서구  남성  중년   5071
...    ...  ..  ..    ...
14674   서구  남성  초년   2793
14677   서구  남성  초년   3082
14684   서구  남성  노년  13158
14687   서구  여성  중년   2809
14688   서구  여성  중년   4025

[689 rows x 4 columns]
{'서구'}
- 데이터 시각화
# barplot으로 여러 성별을 한눈에 볼 수 있게 -> 전체
sns.barplot(x='성별', y = '평균소득',hue = '연령별', 
data=data_sg, palette='BuGn', alpha=0.7 )
plt.title('서구 연령별 평균소득 비교')
plt.show()

(3). TV선호도

--결론 :
우리는 이러한 통계 지표를 통해 타겟 광고를 실현 할 수 있다. 예를들어, 연수구의 경우 남성이용객이 여성 이용객보다 많으며 40,50대가 주류를 이룬다. 또한, 평균소득이 높은 지역이므로 40, 50대 남성 중에서도 소득이 높은 고객을 타겟으로 한 광고가 좋은 효과를 거둘 것으로 기대된다. 따라서 자동차, 전자기기, 골프 등 사치품과 전자 제품 광고를 유치하는 노력이 필요하겠다.

0개의 댓글