"""
# 타슈 데이터 분석
"""
%matplotlib inline
import pandas as pd
import datetime
import numpy as np
import matplotlib.image
import matplotlib.pyplot as plt
from matplotlib import cm
pd.__version__
"""
## Open data with  csv file
"""
rent = pd.read_csv('../input/tashu-taejon-shared-bike-data/2013_01.csv', parse_dates=[2,4])
rent.dtypes
rent.describe()
rent.head()
# NaN 셀을 0으로 바꾸기
rent['rent_station'] = rent.rent_station.fillna(0)
rent['return_station'] = rent.return_station.fillna(0)
rent.head()
# float 를 int로 바꾸기
rent['rent_station']=rent.rent_station.astype(int)
rent.head()
rent['return_station']=rent.return_station.astype(int)
rent.head()
rent.dtypes
"""
### 대여 시간 계산하기
"""
rent['sub'] = rent.return_date - rent.rent_date
rent['sub'].astype('timedelta64[s]')
rent[:10]
ts = pd.Series(1, rent['rent_date'])
ts[:10]
#hourly_ts = ts.resample('H').count()
hourly_ts = ts.groupby(ts.index.hour).sum()
hourly_ts.head()
plt.title('Hourly Usage')
hourly_ts.plot()
plt.show()
daily_ts = ts.resample('D').count()
daily_ts[:10]
monthly_ts = ts.resample('M').count()
monthly_ts[:10]
plt.title('Daily Usage')
daily_ts.plot()
plt.show()
plt.title('Monthly Usage')
monthly_ts.plot()
"""
## weekly stat
"""
rent['weekday'] = pd.DatetimeIndex(rent['rent_date']).weekday
rent[:10]
rent_weekday = rent.groupby('weekday').rent_station.count()
rent_weekday
date_number = [0,1,2,3,4,5,6]
date_labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
plt.title('Day of  Week Rentals')
#rent_weekday.plot()
rent_weekday.plot(kind='bar', 
                  color=['r', 'g', 'b', 'k', 'y', 'm', 'c']); 
                  
#plt.axhline(y=0, color='k')
plt.xticks(date_number, date_labels, rotation=45)
#plt.hist(rent_weekday)
plt.show()
from numpy import *
rent[:10]
rent['hour'] = rent['rent_date'].dt.hour
rent.head()
rent['count'] = 1
weekly_hourly = rent.groupby(['weekday', 'hour']).size().unstack()
weekly_hourly
weekly_hourly.fillna(0, inplace=True)
weekly_hourly
weekly_hourly = weekly_hourly.astype(int)
weekly_hourly
heatmap_hr = np.zeros([7,24])
import seaborn as sns
hm = sns.heatmap(weekly_hourly, cmap="YlGnBu")
"""
## Top 10 stations ?
"""
rent_station_cnt = rent.groupby('rent_station').rent_station.count()
rent_station_cnt[:10]
rent_station_cnt.sort_values(inplace=True, ascending=False)
top10 = rent_station_cnt[:10]
top10
import pandas as pd
station_df = pd.read_csv(r'../input/tashu-taejon-shared-bike-data/station_utf8.csv')
station_df.head(5)
top10
top10.index
top10.dtypes
top10_df = top10.to_frame()
top10_df
top10_df.rename(columns={"rent_station":"count"}, inplace=True)
top10_df
top10_df["no"] = top10_df.index
top10_df
top10_df.rename(columns={"station_no":"no"}, inplace=True)
top10_df
station_df.head()
top10_merge_df = pd.merge(left=top10_df, right=station_df, how="inner", on="no")
top10_merge_df
import folium
 
m = folium.Map(location=[36.369855, 127.388749],
               zoom_start=12)
m
#import folium
%matplotlib inline
locationlist = list(zip(top10_merge_df["lat"], top10_merge_df["lon"]))
locationlist
m = folium.Map(location=locationlist[0],
               zoom_start=13)
for i in range(10):
    folium.Marker(locationlist[i], popup=locationlist[i]).add_to(m)
m