-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piyush.py
54 lines (48 loc) · 1.54 KB
/
Piyush.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pandas as pd
import numpy as np
from datetime import datetime
import calendar
import seaborn as sns
import matplotlib.pyplot as plt
uniroute=[]
def data_preparation(data):
days,years = findDay(data['date'])
data['days']=days
data['year']=years
uniroute=data.route.unique()
dgrp=data.groupby(['route','daytype','days','year']).sum()
newDF = pd.DataFrame()
notweek=[]
inweek=[]
# dgrp=dgrp.unstack()
dgrp=dgrp.reset_index()
for every in uniroute:
dgrpr1=dgrp.loc[(dgrp['route']==every) & (dgrp['daytype']=='W')]
size=dgrpr1.shape[0]
if (size == 95):
newDF=newDF.append(dgrpr1)
else:
notweek.append(every)
return newDF
def createplot(newDF):
years=[2013,2014,2015,2016,2017,2018,2019]
nndf= newDF[newDF.year.isin(years)]
nndf=nndf.groupby(['days','year']).mean()
nndf=nndf.reset_index()
y=sns.barplot(x="days",y="rides",hue="year",data=nndf,palette="plasma")
plt.legend(title='Year',loc='center left',bbox_to_anchor=(1,0.5))
plt.xlabel("Weekdays")
plt.ylabel("Mean ridership for each year")
plt.title("Mean Bus ridership for weekdays from 2013 - 2019")
plt.show()
def findDay(dd):
daylist=[]
yearlist=[]
for date in dd:
month, day, year = (int(i) for i in date.split('/'))
dayNumber = calendar.weekday(year, month, day)
days =["Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"]
daylist.append(days[dayNumber])
yearlist.append(year)
return daylist,yearlist