# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
from reader import load_dataframe
import pandas as pd
import numpy as np
import os
import glob
from datetime import date, datetime
# %matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import requests
import math
import matplotlib.ticker as mtick

plot_colors=["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71", '#ffce44']
# -

# df = load_dataframe()
df = pd.read_csv('ny_sf_all_data_0703.csv', parse_dates=[1,2])
df.sample(5)

#convert start time to hour decimal to graph
# df['start hour'] = df['starttime'].apply(lambda x: x.hour + (x.minute/60))
df['start hour'] = df['starttime'].apply(lambda x: x.hour + (round((x.minute/60)*4))/4) #rounds to quarter hour
# df['age'] = df['age'].fillna('Other')
df.head()

#age grouping
age_bins = [0, 20, 30, 40, 50, 60, 100]
age_groups = ['0-20', '21-30', '31-40', '41-50', '51-60', '61+']
df['age groups'] = pd.cut(df['age'], age_bins, labels=age_groups)
df.sample(5)

# ### age groups, average age

df.groupby('age groups').agg(['count', 'median'])['start hour']

#create data frame for each city
grouped = df.groupby('city')
ny = grouped.get_group('New York')
sf = grouped.get_group('San Francisco')

avg_age = df['age'].mean()
sf_avg_age = sf['age'].mean()
ny_avg_age = ny['age'].mean()
print('the overall average age is: {}'.format(avg_age))
print('the average age in New York is: {}'.format(ny_avg_age))
print('the average age in San Francisco is: {}'.format(sf_avg_age))

age_group_count = df.groupby('city')['age groups'].value_counts()
age_group_count = pd.DataFrame([age_group_count]).transpose()
age_group_count
# ny_age = ny['age groups'].value_counts().sort_index()
# sf_age = sf['age groups'].value_counts().sort_index()
# print(ny_age)
# print(sf_age)


# +
#NEED TO GRAPH AGE GROUPS

# plt.figure(figsize=(10,8))
# age_group_count.plot(kind='bar')
# # sf_plt = plt.bar(sf_age['index'], sf_age['age groups'])
# # ny_plt = plt.bar(ny_age['index'], ny_age['age groups'])

# plt.title('Bike Users by Age', fontsize=18)
# plt.xlabel("Age Group", fontsize = 14)
# plt.ylabel("Number of Users", fontsize = 14)
# # plt.xticks(np.arange(6),('0-20', '21-30','31-40','41-50','51-60', '61+'))
# plt.xticks(np.arange(6), age_groups)
# plt.show()
# -



# ### count by age group, by city

#NY age group breakdown
try:
    ny_age_group = ny.groupby('age groups').count()['usertype'].reset_index()
except KeyError:
    pass
ny_age_group

#SF age group break down
try:
    sf_age_group = sf.groupby('age groups').count()['usertype'].reset_index()
except KeyError:
    pass
sf_age_group

ny_age_gender = ny.groupby(['age groups', 'gender']).gender.count().unstack().fillna(0)
ny_age_gender =ny_age_gender.drop(['Unknown'], axis =1).reset_index()
ny_age_gender = ny_age_gender[['age groups', 'Male', 'Female']]
ny_age_gender


# age group break down by gender NY
plot_colors=['#9b59b6', '#34495e', '#95a5a6']
ny_age_gender.plot(kind='bar', color =plot_colors, stacked=True, figsize=(10,8))
plt.legend(fontsize=14)
plt.title('New York Users by Age & Gender', fontsize=18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Users", fontsize = 14)
plt.xticks(np.arange(6),('0-20', '21-30','31-40','41-50','51-60', '61+'), fontsize= 14, rotation =0)
plt.show()

sf_age_gender = sf.groupby(['age groups', 'gender']).gender.count().unstack().fillna(0)
sf_age_gender =sf_age_gender.drop(['Other'], axis =1).reset_index()
sf_age_gender

# age group break down by gender SF
plot_colors=['#9b59b6', '#34495e', '#95a5a6']
sf_age_gender.plot(kind='bar', color =plot_colors, stacked=True, figsize=(10,8))
plt.legend(fontsize=14)
plt.title('San Francisco Users by Age & Gender', fontsize=18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Users", fontsize = 14)
plt.xticks(np.arange(6),('0-20', '21-30','31-40','41-50','51-60', '61+'), fontsize= 14, rotation =0)
plt.show()

#NewYork user count by age group
plt.figure(figsize=(10,8))
plt.bar(ny_age_group['age groups'], ny_age_group['city'], color='#34495e', align="center")
plt.title("New York Bike Users, By Age", fontsize = 18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Users", fontsize = 14)

#SanFran user count by age group
plt.figure(figsize=(10,8))
plt.bar(sf_age_group['age groups'], sf_age_group['usertype'], color='#9b59b6', align="center")
plt.title("San Francisco Bike Users, By Age", fontsize = 18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Users", fontsize = 14)

# ### time of day by age

df['start date'], df['start time'] = df['starttime'].str.split(' ',1).str
df['end date'], df['end time'] = df['stoptime'].str.split(' ', 1).str
# df.drop(['starttime', 'stoptime'], axis = 1)
df.head()

sf['tripduration'].hist(bins=100)

sf.groupby('age groups').count()

zero = sf[sf['age groups'] == '0-20']
twenty1= sf[sf['age groups'] == '31-40' ]
forty1 = sf[sf['age groups'] == '41-50' ]
fifty1 = sf[sf['age groups'] == '51-60' ]
sixty1 = sf[sf['age groups'] == '61+' ] 
thirty1 = sf[sf['age groups'] == '21-30' ]

plt.figure(figsize=(20,8))
sns.set(rc={'lines.linewidth':4.5}, font_scale=2)
sns.distplot(zero['start hour'], hist=False, color ='#95a5a6', label = '0-20')
sns.distplot(twenty1['start hour'], hist=False, color ='#9b59b6', label = '21-30')
sns.distplot(thirty1['start hour'], hist=False, color = '#3498db', label = '31-40')
sns.distplot(forty1['start hour'], hist=False, color = '#e74c3c',label = '41-50')
sns.distplot(fifty1['start hour'], hist=False, color = '#34495e',label = '51-60')
sns.distplot(sixty1['start hour'], hist=False, color = '#2ecc71',label = '61+')
plt.legend()
plt.title("Start Time by Age Group_San Francisco")
plt.xlabel("Time")
plt.xticks(np.arange(0, 25, step=4), ('midnight', '4am', '8am', 'noon', '4pm', '8pm', 'midnight'))
plt.ylabel("User Count")
plt.show()


# ### percent users by city & age group

# +
#SanFran
sf_total_riders = sf['usertype'].count()
print('total number of users in SanFran is: {}'.format(sf_total_riders))

sf_age_group['age group percent'] = sf_age_group['usertype'].map(lambda x: (x/sf_total_riders)*100)

sf_age_group['age group percent'] = sf_age_group['age group percent'].map('{0:.2f}%'.format) 

#export to excel to handle "other" category for non-reporting
sf_age_group.to_clipboard(excel=True)
sf_age_group_df = pd.read_csv('sf_age_group_df.csv')
sf_age_group_df

# -

sns.set()
sf_age_group_df.plot(kind='bar', color = plot_colors, figsize=(10,8), legend=False)
plt.title('Age Group breakdown San Francisco', fontsize=18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Riders", fontsize = 14)
plt.xticks(np.arange(7),('0-20', '21-30','31-40','41-50','51-60', '61+', 'other'), fontsize= 14, rotation=0)
plt.show()

# +
sns.set()

age_0 = float(sf_age_group_df.iloc[0]['usertype'])
age_21 = sf_age_group_df.iloc[1]['usertype']
age_31 = sf_age_group_df.iloc[2]['usertype']
age_41 = sf_age_group_df.iloc[3]['usertype']
age_51 = sf_age_group_df.iloc[4]['usertype']
age_61 = sf_age_group_df.iloc[5]['usertype']
age_other = sf_age_group_df.iloc[6]['usertype']

colors = plot_colors
labels = '0-20', '21-30','31-40','41-50','51-60', '61+', 'other'
ages = [age_0, age_21, age_31, age_41, age_51, age_61, age_other]


plt.pie(ages, labels=labels, colors=plot_colors, startangle=90)
plt.title('Age Group breakdown San Francisco', fontsize=18)
plt.axis('equal')
plt.show()

# +
#NewYork
ny_total_riders = ny['usertype'].count()
print('total number of users in New York is: {}'.format(ny_total_riders))

ny_age_group['age group percent'] = ny_age_group['usertype'].map(lambda x: (x/ny_total_riders)*100)

ny_age_group['age group percent'] = ny_age_group['age group percent'].map('{0:.2f}%'.format) 

#export to excel to handle "other" category for non-reporting
ny_age_group.to_clipboard(excel=True)
ny_age_group_df = pd.read_csv('ny_age_group_df.csv')
ny_age_group_df

# -

sns.set()
ny_age_group_df.plot(kind='bar', color = '#34495e', figsize=(10,8), legend=False)
plt.title('Age Group breakdown New York', fontsize=18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Riders", fontsize = 14)
plt.xticks(np.arange(7),('0-20', '21-30','31-40','41-50','51-60', '61+', 'other'), fontsize= 14,rotation=0)
plt.show()

# +
sns.set()

age_0 = float(ny_age_group_df.iloc[0]['usertype'])
age_21 = ny_age_group_df.iloc[1]['usertype']
age_31 = ny_age_group_df.iloc[2]['usertype']
age_41 = ny_age_group_df.iloc[3]['usertype']
age_51 = ny_age_group_df.iloc[4]['usertype']
age_61 = ny_age_group_df.iloc[5]['usertype']
age_other = ny_age_group_df.iloc[6]['usertype']

colors = plot_colors
labels = '0-20', '21-30','31-40','41-50','51-60', '61+', 'other'
ages = [age_0, age_21, age_31, age_41, age_51, age_61, age_other]


plt.pie(ages, labels=labels, colors=plot_colors, startangle=90)
plt.title('Age Group breakdown New York', fontsize=18)
plt.axis('equal')
plt.show()
# -

# testing customer type by age - works
ny_user_type = ny.groupby('usertype')['age groups'].value_counts()
ny_user_type

try:
    ny_age_group = ny.groupby('age groups').count()['city'].reset_index()
except KeyError:
    pass

# ### user type by age

# +
#NewYork
ny_total_riders = ny['usertype'].count()
print('total number of users in New York is: {}'.format(ny_total_riders))

ny_usertype = ny.groupby('usertype')['age groups'].value_counts(normalize=True)
ny_usertype
# -

ny_usertype.unstack(0).plot(kind='bar', figsize=(10,8))
plt.legend(fontsize=14)
plt.title('Customer vs Subscriber - New York', fontsize=18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Users", fontsize = 14)
plt.gca().set_yticklabels(['{:.0f}%'.format(x*100) for x in plt.gca().get_yticks()], fontsize=12) 
plt.xticks(np.arange(6),('0-20', '21-30','31-40','41-50','51-60', '61+'), fontsize= 12, rotation=0)
plt.show()

# +
#San Fran Customer Type
sf_total_riders = sf['usertype'].count()
print('total number of users in San Francisco is: {}'.format(ny_total_riders))

sf_usertype = sf.groupby('usertype')['age groups'].value_counts(normalize=True)

sf_usertype
# -

ny_usertype.unstack(0).plot(kind='bar', figsize=(10,8))
plt.legend(fontsize=14)
plt.title('Customer vs Subscriber - New York', fontsize=18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Number of Users", fontsize = 14)
plt.gca().set_yticklabels(['{:.0f}%'.format(x*100) for x in plt.gca().get_yticks()], fontsize=12) 
plt.xticks(np.arange(6),('0-20', '21-30','31-40','41-50','51-60', '61+'), fontsize= 12, rotation=0)
plt.show()

# ### avg trip duration by age

#NewYork
ny_trip_duration = ny.groupby('age groups')['tripduration'].mean().reset_index()
# ny_trip_duration['ny tripduration'] = ny_trip_duration['tripduration'].map(lambda x: x/60)
ny_trip_duration


#SanFran
sf_trip_duration = sf.groupby('age groups')['tripduration'].mean().reset_index()
# sf_trip_duration['sf tripduration'] = sf_trip_duration['tripduration'].map(lambda x: x/60)
sf_trip_duration

# +
trip_duration =sf_trip_duration.join(ny_trip_duration, lsuffix='_sf', rsuffix='_ny')
trip_duration['tripduration_sf'] = trip_duration['tripduration_sf'].map(lambda x: x/60)
trip_duration['tripduration_ny'] = trip_duration['tripduration_ny'].map(lambda x: x/60)

trip_duration
# -

trip_duration.plot(kind='bar', color = plot_colors, figsize=(10,8))
plt.legend(fontsize=14)
plt.title('New York Trip duration by age group', fontsize=18)
plt.xlabel("Age Group", fontsize = 14)
plt.ylabel("Average Ride Duration (in minutes)", fontsize = 14)
plt.xticks(np.arange(6),('0-20', '21-30','31-40','41-50','51-60', '61+'), fontsize= 14, rotation=0)
plt.show()

# ### popular stations by age

ny_drop = ny.dropna(axis=0)
start_station = ny_drop.groupby('age groups')['start station name'].value_counts()
start_station = start_station.groupby('age groups').nlargest(5)
# start_station.plot(kind='bar', stacked=True, figsize=(10,8))

start_station = pd.DataFrame(start_station)
start_station.index.names=['age group', 'x', 'start station']
start_station.reset_index(inplace=True)
start_station= start_station[['age group', 'start station', 'start station name']]
start_station

# ### bikes around the world

url = 'https://bikeshare-research.org/api/v1/categories/base/fields/country'
response = requests.get(url).json()

df= pd.DataFrame([x for x in response])
country_count = df.groupby('country').count().reset_index()

country_count = country_count.sort_values(by=['bssid'])
top5 = country_count.nlargest(5, 'bssid')
top5

# +
# cool live map of bikes in use
# http://bikes.oobrien.com/#zoom=3&lon=-16.1133&lat=37.9504

StarcoderdataJupyterScriptsDedupFiltered