1. 따릉이 API
- import
import requests
import folium
import json
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
- 데이터 요청하기
request = requests.post(targetSite, data={'stationGrpSeq':'ALL'})
print(request)
# print(request.text)
<Response [200]> |
- json 데이터 처리하기
jsonload(): json타입의 문자열 데이터를 파이썬에서 처리할 수 있도록 변환(딕셔너리로 변환)
bike_json = json.loads(request.text)
print(bike_json)
print(type(bike_json))
{'stationImgPath': '/nas_link/spb/attachFiles/file_admin/basePath', 'appUserSessionVO': {'lang': 'LAG_001', 'regDttm': None, 'encPwd': None, 'usrClsCd': None, 'usrDeviceId': None, 'loginId': None, 'mbId': None, 'mpnLostYn': None, 'snsType': None, 'usrSeq': None, 'voucherEndDttm': None, 'voucherSeq': None, 'usrType': None, 'usrMpnNo': None, 'appOsType': None, 'orgType': None, 'snsId': None, 'viewFlg': 'list', 'usrBirthDate': None, 'sexCd': None, 'rentEncPwd': None, 'telecomClsCd': None, 'authCiVal': None, 'authClsCd': None, 'mbTelNo': None, 'mbEmailName': None, 'mbPostNo': None, 'mbAddr1': None, 'mbAddr2': None, 'parentSexCd': None, 'parentBirthDate': None, 'parentMpnNo': None, 'emailSendAgreeYn': None, 'lastLoginDttm': None, 'mbWgt': None, 'langClsCd': None, 'leaveYn': None, 'leaveReasonCd': None, 'leaveDttm': None, 'mbInfoColecAgreeDttm': None, 'mpnLostDttm': None, 'pagingYn': None, 'usrIp': None, 'partyVoucherSeq': None, 'elecVoucherSeq': None, 'requestSeq': None, 'usrDeviceType': None, 'authDiVal': None, 'snsEmail': None, 'tCardYn': None, 'mCardYn': None, 'mainType': None, 'pageSize': 0, 'currentPageNo': 1, 'recordCountPerPage': 0, 'totalRecordCount': 0, 'firstRecordIndex': 0, 'searchValue': None, 'searchParameter': None, 'searchDate': None, 'searchStartDate': None, 'searchEndDate': None, 'mode': None}, 'loginYn': 'N', 'realtimeList': [{'stationName': '102. 망원역 1번출구 앞', 'stationImgFileName': '', 'stationId': 'ST-4', 'stationLongitude': '126.91062927', 'stationLatitude': '37.55564880', 'rackTotCnt': '15', 'parkingBikeTotCnt': '0', 'parkingQRBikeCnt': '5', 'parkingELECBikeCnt': '11', 'stationSeCd': 'RAK_002', 'mode': None} ......... {'stationName': '6173. 서울자동차운전전문학원', 'stationImgFileName': '', 'stationId': 'ST-3284', 'stationLongitude': '126.82106018', 'stationLatitude': '37.54613495', 'rackTotCnt': '10', 'parkingBikeTotCnt': '0', 'parkingQRBikeCnt': '18', 'parkingELECBikeCnt': '0', 'stationSeCd': 'RAK_002', 'mode': None}], 'sessionId': '84006A2081CABE74E36964CD33D28D5F.spbSvcWeb2UserWeb', 'stationVO': {'lang': 'LAG_001', 'usrSeq': None, 'stationName': None, 'stationImgFileName': None, 'stationId': None, 'rackUsableCnt': None, 'bkmkSeq': None, 'stationUseCnt': None, 'tabId': None, 'bkmk': None, 'stationBkmkSeq': None, 'stationLongitude': None, 'stationLatitude': None, 'rackTotCnt': None, 'parkingBikeTotCnt': None, 'parkingQRBikeCnt': None, 'parkingELECBikeCnt': None, 'stationSeCd': None, 'pageYn': 'Y', 'bikeTotCnt': None, 'locateNo': None, 'rackRate': None, 'parkingRackTotCnt': None, 'shared': None, 'parkingRack': None, 'parkingBike': None, 'criticalFlg': None, 'closeFlg': None, 'systemWarning': None, 'currnetStatus': None, 'stationAddr': None, 'stationGrpSeq': 'ALL', 'stationPostNo': None, 'stationClsCd': None, 'stationClsName': None, 'brokenApFlg': None, 'brokenBikeFlg': None, 'brokenArmFlg': None, 'stationUseYn': None, 'pageSize': 0, 'currentPageNo': 1, 'recordCountPerPage': 0, 'totalRecordCount': 0, 'firstRecordIndex': 0, 'searchValue': None, 'searchParameter': None, 'searchDate': None, 'searchStartDate': None, 'searchEndDate': None, 'mode': None}, 'checkResult': True} <class 'dict'></class 'dict'> |
- 딕셔너리 타입의 데이터를 테이터 프레임으로 변환하기
json_nomalize(): 딕셔너리의 타입의 데이터를 판다스 데이터프레임으로 변환
bike_df = pd.json_normalize(bike_json, 'realtimeList')
bike_df
- 컬럼확인
bike_df.columns
* stationName : 대여소 이름
* stationId : 고유한 대여소 번호
* stationLongitude : 대여소 경도
* stationLatitude : 대여소 위도
* rackTotCnt : 주차 가능한 전체 자전거 대수
* parkingBikeTotCnt : 주차된 따릉이 총 대수
* parkingQRBikeCnt : 주차된 따릉이 QR형 총 대수
* parkingELECBikeCnt : 주차된 새싹 따릉이 총 대수
Index(['stationName', 'stationImgFileName', 'stationId', 'stationLongitude', 'stationLatitude', 'rackTotCnt', 'parkingBikeTotCnt', 'parkingQRBikeCnt', 'parkingELECBikeCnt', 'stationSeCd', 'mode'], dtype='object') |
- 필요한 데이터만 넣기
bike_df_map = bike_df[['stationName', 'stationId', 'stationLongitude', 'stationLatitude', 'rackTotCnt', 'parkingBikeTotCnt', 'parkingQRBikeCnt', 'parkingELECBikeCnt']]
bike_df_map
- 위도, 경도 -> float 변환
# 위도, 경도 -> float 변환
bike_df_map['stationLongitude'] = bike_df_map['stationLongitude'].astype(float)
bike_df_map['stationLatitude'] = bike_df_map['stationLatitude'].astype(float)
- 주차할 수 있는 자전거 대수,
주차된 자전거 총 대수,
주된 QR 자전거 총 대수,
주차된 새싹 자전거 총 대수 -> int 형으로 변환
# 주차할 수 있는 자전거 대수, 주차된 자전거 총 대수,
bike_df_map['rackTotCnt'] = bike_df_map['rackTotCnt'].astype(int)
bike_df_map['parkingBikeTotCnt'] = bike_df_map['parkingBikeTotCnt'].astype(int)
bike_df_map['parkingQRBikeCnt'] = bike_df_map['parkingQRBikeCnt'].astype(int)
bike_df_map['parkingELECBikeCnt'] = bike_df_map['parkingELECBikeCnt'].astype(int)
- 파생변수 만들기[total] : 따릉이 + QR + 새싹
# 파생변수 만들기[total] : 따릉이 + QR + 새싹
bike_df_map['total']
= bike_df_map['parkingBikeTotCnt'] + bike_df_map['parkingQRBikeCnt'] + bike_df_map['parkingELECBikeCnt']
- 데이터 확인하기
bike_df_map.dtypes
bike_df_map.head()
bike_df_map.shape
|
- 지도에서 자전거 대수 뜨게 생성하기
bike_map = folium.Map(location=[bike_df_map['stationLatitude'].mean(),
bike_df_map['stationLongitude'].mean()],
zoom_start=12)
for index, data in bike_df_map.iterrows():
popup_str = '{} 일반:{}대, QR:{}대, 새싹:{}대, 총:{}대'.format(
data['stationName'], data['parkingBikeTotCnt'], data['parkingQRBikeCnt'],
data['parkingELECBikeCnt'], data['total']
)
popup = folium.Popup(popup_str, max_width=600)
folium.Marker(location=[data['stationLatitude'], data['stationLongitude']],
popup=popup).add_to(bike_map)
bike_map
|
'데이터분석 > 실습' 카테고리의 다른 글
06. 데이터분석5 : 떡볶이 프렌차이즈의 입점전략 (0) | 2024.06.03 |
---|---|
04. 데이터분석3 : 전국 도시공원 데이터 (0) | 2024.05.28 |
03. 데이터분석2 : 상권별 업종 밀집 통계 데이터 (0) | 2024.05.28 |
02. 데이터분석1 : 가상 온라인 쇼핑몰 데이터 활용 (0) | 2024.05.27 |
01. Matplotlib (0) | 2024.05.27 |