<jupyter_start><jupyter_text>For this homework, you are a data scientist working for Pronto (before the end of their contract with the City of Seattle). Your job is to assist in determining how to do end-of-day adjustments in the number of bikes at stations so that all stations will have enough bikes for the next day of operation (as estimated by the weekday average for the station for the year). Your assistance will help in constructing a plan for each day of the week that specifies how many bikes should be moved from each station and how many bikes must be delievered to each station.Your assignment is to construct plots of the differences between 'from' and 'to' counts for each station by day of the week. Do this as a set of 7 subplots. You should use at least one function to construct your plots. Grading2-pts: create a dataframe with station counts averages by day-of-week1-pt: structure the 7 day-of-week plots as subplots1-pt: label the plots by day-of-week1-pt: label the x-axis for plots in the last row and label the y-axis for plots in the left-most column<jupyter_code>import pandas as pd 
import matplotlib.pyplot as plt
%matplotlib inline 
import numpy as np

trips = pd.read_csv('2015_trip_data.csv',
                    parse_dates=['starttime', 'stoptime'],
                    infer_datetime_format=True)
trips.head()
time=trips['starttime']=pd.to_datetime(trips['starttime'])
trips['stoptime']=pd.to_datetime(trips['stoptime'])


trips['dayofweek'] = trips['starttime'].dt.dayofweek

df = pd.DataFrame({'start' : pd.to_datetime(trips['starttime'][0]),
                   'end' : pd.to_datetime(trips['starttime'][len(trips) - 1])}, index = [0])

(df['end']-df['start']).apply(lambda x: x/np.timedelta64(1,'W'))

names=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
fromlist=[]
for i in range(0,7):
    subset = trips[trips.dayofweek == i]
    from_station_counts = pd.value_counts(subset['from_station_id'])
    from_station_mean = pd.DataFrame({name[i]: from_station_counts/52})
    from_station_mean = round(from_station_mean,2)
    fromlist.append(from_station_mean)
    
df_from = pd.concat(fromlist, axis=1)  
df_from.head()
mean_list=[]
for i in range(0,7):
    subset = trips[trips.dayofweek == i]
    from_station_counts = pd.value_counts(subset['from_station_id'])
    to_station_counts = pd.value_counts(subset['to_station_id'])
    station_mean_diff = pd.DataFrame({name[i]: from_station_counts/52-to_station_counts/52})
    station_mean_diff = round(station_mean_diff,4)
    mean_list.append(station_mean_diff)
    
df_diff_mean= pd.concat(mean_list, axis=1)  
df_diff_mean.head()
def plot_diff(df_diff_mean,names):
    bar_width=0.4 
    opacity=0.5 
    len_names=len(names)
    len_df_diff_mean=len(df_diff_mean.index)
    index=np.arange(len_df_diff_mean)
    
    for name in names: 
        i=++1 
        fig=plt.figure(figsize=(12,20))
        plt.subplot(len_names,1,i)
        rects1=plt.bar(index,df_diff_mean[name],bar_width,alpha=opacity,color='r',label='Differece in Average')
        plt.xticks(index, df_from.index)
        _, labels = plt.xticks()
        plt.setp(labels, rotation=90)
        plt.xlabel('station')
        plt.ylabel('average count')
        plt.title(name)
        plt.legend()
        plt.show()
    
plot_diff(df_diff_mean,names)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup