【发布时间】:2018-02-24 08:52:57
【问题描述】:
我在这个文件中有数据https://drive.google.com/open?id=0B4KXs5bh3CmPWXJkQWhkbzI0WEE
#count weekdays 0 to 4
mUserType = np.where(rides['starttime'].dt.dayofweek <= 5, 'Weekend',
'Weekday')
#count hours
mWeekHours = rides['starttime'].dt.hour
#create group by usertype
WeekdayWorkdayResult = rides.groupby([mUserType, mWeekHours,
'usertype']).size().unstack()
WeekdayWorkdayResult
此代码根据用户类型(即客户和订阅者)计算周末和工作日的小时数
我想用 matplotlib 以这种格式绘制图形怎么办?Weekday and Weekend Graph
按小时和用户类型显示乘车次数。一个情节用于工作日,第二个情节用于周末。包括所有每小时骑行的情节线
# count trips by date and by hour
ind = pd.DatetimeIndex(rides.starttime)
rides['date'] = ind.date.astype('datetime64')
rides['hour'] = ind.hour
by_hour = rides.pivot_table('tripduration', aggfunc='count',
index=['date', 'hour'],
columns='usertype').fillna(0).reset_index('hour')
# average these counts by weekend
by_hour['weekend'] = (by_hour.index.dayofweek >= 5)
by_hour = by_hour.groupby(['weekend', 'hour']).mean()
by_hour.index.set_levels([['weekday', 'weekend'],
["{0}:00".format(i) for i in range(24)]],
inplace=True);
by_hour.columns.name = None
fig, ax = plt.subplots(1, 2, figsize=(16, 6), sharey=True)
by_hour.loc['weekday'].plot(title='Hourly Rides User Type and Hour',
ax=ax[0])
by_hour.loc['weekend'].plot(title='Weekend Rides by User Type and Hour',
ax=ax[1])
ax[0].set_ylabel('Hourly Rides by User Type');
这是我只获取订阅者和客户数据的代码,我还想获取所有图表线表示周末和工作日的子和客户的小时总和
【问题讨论】:
标签: python pandas matplotlib