Python:数据透视表错误

时间:2016-01-16 09:32:32

标签: python numpy pandas

我试图在周末和工作日找到average hourly trips这两个"年度会员"和"短期通行证持有人"

数据框信息:

DatetimeIndex: 7795 entries, 2014-10-13 to 2015-10-12
Data columns (total 4 columns):
(hour, )                             7795 non-null int64
(trip_id, Annual Member)             7795 non-null float64
(trip_id, Short-Term Pass Holder)    7795 non-null float64
(weekend, )                          7795 non-null bool

数据框看起来像附加图像 enter image description here

我尝试了以下代码,但它无效      by_hour.pivot_table(index=['weekend','hour'],aggfunc ='mean',columns=['Annual Member','Short-Term Pass Holder'])

引发的错误是:

  

AttributeError:' numpy.ndarray'对象没有属性' start'

编辑:发布完成的代码:

%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns; sns.set()
trips = pd.read_csv('2015_trip_data.csv', parse_dates=['starttime', 'stoptime'],
                infer_datetime_format=True)
 ind = pd.DatetimeIndex(trips.starttime)
 trips['date'] = ind.date.astype('datetime64')
 trips['hour'] = ind.hour
 by_date = trips.pivot_table(index=['date'],values =['trip_id'], columns='usertype', aggfunc ='count')
 by_weekday=by_date.groupby([by_date.index.year,by_date.index.dayofweek]).mean()
 by_hour = trips.pivot_table(index =['date','hour'], columns =['usertype'],
                        values =['trip_id'], aggfunc ='count').fillna(0).reset_index('hour')

0 个答案:

没有答案