카테고리 없음

파이썬 데이터 분석 예제 _ 지하철 시간대 별 승하차 데이터 분석

wangatheringdata 2025. 3. 8. 20:34

 

 

 

 

파이썬

 

 

 

 

 

1. 개요

지하철 시간대 별 승하차 인원 추이

1. 전체 역의 시간대 별 승차 및 하차 인원 각각 누적 집계
2. 승하차 별 시각화

 

 


2. 모듈 import 및 데이터 읽기

import csv
import matplotlib.pyplot as plt

f = open('./data/subwaytime.csv')
data = csv.reader(f)

next(data)
next(data)

    header 총 2개 행,

    0행 : 사용월, 호선명, 역ID, 지하철역 표시 / AM4:00 ~ AM3:00까지 1시간 단위로 구분

    1행 : 공백('') 4개와 승차와 하차 번갈아 표시 / header  2줄은 분석에 영향 없어 next()함수로 제외 (2회)

 

 

 

3. 승하차 별 데이터 저장

3-1. 각 시간의 승하차 데이터 저장 / 24시간

import csv
import matplotlib.pyplot as plt

f = open('./data/subwaytime.csv')
data = csv.reader(f)

next(data)
next(data)

s_in = [0] * 24
s_out = [0] * 24
'''
Out[7]: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
'''

 

 

3-2. 24시간 데이터를 순서대로 list로 저장

    각 행의 index 4번부터 마지막까지의 데이터까지는 map() 함수로 한꺼번에 정수로 변환 :
    map(변환함수index, 변환데이터value)
    
변환함수index  =>  int() 함수
변환데이터value  => CSV 파일에서 불러온 데이터의 각 행의 index 4번부터 마지막까지

import csv
import matplotlib.pyplot as plt

f = open('./data/subwaytime.csv')
data = csv.reader(f)

next(data)
next(data)

s_in = [0] * 24
s_out = [0] * 24

for row in data :
    row[4:] = map(int, row[4:])

 

 

3-3.  모든 역 시간대 별 승차하차 인원 누적

1시간 마다 승차인원index는 2씩 증가

csv파일 상 승차는 4번, 하차는 5번 index에서 시작

승차시각index(t)와 승하차인원index(i)의 관계 =>  i = 4 + (t – 4) * 2 / i = 5 + (t – 5) * 2

import csv
import matplotlib.pyplot as plt

f = open('./data/subwaytime.csv')
data = csv.reader(f)

next(data)
next(data)

s_in = [0] * 24
s_out = [0] * 24

for row in data :
    row[4:] = map(int, row[4:])
    
    for i in range(24) :
        s_in[i] += row[4 + i * 2]
        s_out[i] += row[5 + i * 2]



 

4. 시각화

    파일 상 첫 운행 시간은 4시 => time = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,0,1,2]

import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
import platform
if platform.system() == 'Darwin' :
    rc('font', family = 'AppleGothic')
elif platform.system() == 'Windows' :
    path = 'c:/Windows/Fonts/malgun.ttf'
    font_name = font_manager.FontProperties(fname=path).get_name()
    rc('font', family=font_name)
else :
    print('Check your OS System')



plt.figure(dpi = 300)

plt.title('지하철 시간대별 승하차 인원 추이')

plt.plot(s_in, label = '승차')
plt.plot(s_out, label = '하차')
plt.legend()

plt.grid()

time = [4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,0,1,2]
plt.xticks(range(24), time)

plt.show()



 

5. 결론

    출퇴근 시간인 08시와 18시에 가장 많은 승하차가 이뤄짐.