데이터 분석/판다스

[Pandas] 서울시 공공자전거 대여 정보 분석 2

eunnys 2023. 11. 14. 17:54

- 대여정보 읽기
- 대여정보 확인 (결측치 컬럼 등)
- 대여일시, 반납일시를 날짜타입으로 변경
- 일자별로 대여 건수 내림차순으로 정렬하여 출력
    - Series객체.dt.날짜property
- 요일별 대여 건수 확인


대여 정보 데이터 읽기

 

rent_info = pd.read_csv('Seoul_bicycle.csv', encoding='cp949')
rent_info.head()


대여정보 확인

 

rent_info.shape # (678830, 11)
rent_info.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 678830 entries, 0 to 678829
Data columns (total 11 columns):
 #   Column    Non-Null Count   Dtype 
---  ------    --------------   ----- 
 0   자전거번호     678830 non-null  object
 1   대여일시      678830 non-null  object
 2   대여 대여소번호  678830 non-null  int64 
 3   대여 대여소명   678830 non-null  object
 4   대여거치대     678830 non-null  int64 
 5   반납일시      678830 non-null  object
 6   반납대여소번호   678830 non-null  int64 
 7   반납대여소명    678830 non-null  object
 8   반납거치대     678830 non-null  int64 
 9   이용시간      678830 non-null  int64 
 10  이용거리      678830 non-null  int64 
dtypes: int64(6), object(5)
memory usage: 57.0+ MB

 

rent_info.isna().sum()
자전거번호       0
대여일시        0
대여 대여소번호    0
대여 대여소명     0
대여거치대       0
반납일시        0
반납대여소번호     0
반납대여소명      0
반납거치대       0
이용시간        0
이용거리        0
dtype: int64

대여일시, 반납일시를 날짜타입으로 변경

 

rent_info['대여일시'] = pd.to_datetime(rent_info['대여일시'])
rent_info['반납일시'] = pd.to_datetime(rent_info['반납일시'])
rent_info.dtypes
자전거번호               object
대여일시        datetime64[ns]
대여 대여소번호             int64
대여 대여소명             object
대여거치대                int64
반납일시        datetime64[ns]
반납대여소번호              int64
반납대여소명              object
반납거치대                int64
이용시간                 int64
이용거리                 int64
dtype: object

요일별 대여 건수 확인

 

rent_info['요일'] = rent_info['대여일시'].dt.dayofweek
map_data = {0:'월요일', 1:'화요일', 2:'수요일', 3:'목요일', 4:'금요일', 5:'토요일', 6:'일요일'}
rent_info['요일'] = rent_info['요일'].map(map_data)
rent_info.head()

 

rent_info.groupby('요일')['요일'].count().sort_values(ascending=False).apply(lambda x:f'{x:,}건')
요일
토요일    119,574건
금요일    113,660건
화요일    104,938건
월요일     95,045건
목요일     91,159건
수요일     88,768건
일요일     65,686건
Name: 요일, dtype: object

 

# 동일한 결과
rent_info['요일'].value_counts()
요일
토요일    119574
금요일    113660
화요일    104938
월요일     95045
목요일     91159
수요일     88768
일요일     65686
Name: count, dtype: int64

일자별로 대여 건수 내림차순으로 정렬하여 출력

 

rent_info['day'] = rent_info['대여일시'].dt.day
rent_info.groupby('day')['day'].count().sort_values(ascending=False)
# 동일한 결과
rent_info['day'].value_counts()
day
4     34711
5     34526
1     33958
6     33234
2     30848
3     30787
7     30549
12    29970
8     28918
9     27380
11    25721
22    24758
23    23920
26    22771
28    20540
21    20408
27    20299
16    19907
29    19755
14    19662
25    18576
10    18537
20    18117
19    17671
30    17519
13    17118
18    16037
24    11278
15     6271
17     5084
Name: day, dtype: int64

 


대여소 정보와 대여정보 Join

 

- 대여소 정보와 대여정보 join
- join 데이터 정보 확인
- 각 구별 이용시간과  이용거리에 대한 평균, 최대, 최소값 출력

 

rent_bicycle = pd.merge(df, rent_info, left_on='대여소ID', right_on='대여 대여소번호')
rent_bicycle.shape # (677505, 21)
rent_bicycle.head()


# 평균 이용시간 기준 내림차순
rent_bicycle.groupby('대여소_구')[['이용시간','이용거리']].agg(['mean','max','min']).sort_values(('이용시간', 'mean'), ascending=False)