我得到了一个显示点击次数和访问者的网络日志,并且无法从该格式的数据框中绘制一周中某几天的频率:
date
0 2017-06-03 00:07:04
1 2017-06-03 00:07:06
2 2017-06-03 00:07:07
3 2017-06-03 00:07:24
4 2017-06-03 00:07:38
我尝试了这个:
Date_df = pd.DataFrame(dataset.date)
dates = pd.date_range('2017-06-01','2017-06-07', freq='D')
dates_count = Date_df.groupby(Date_df.date).count()['date']
dates_day_count = pd.DataFrame(dates_count)
dates_day_count = dates_day_count.rename(columns={"date":"Counts"})
dates_day_count.index.rename('date', inplace = True)
dates_day_count.tail()
但显示“ KeyError: 'date'
”
我想知道站点一天中最忙的部分(一天中最频繁的时间),有人吗?
我认为需要Series.value_counts
有dt.date
或dt.hour
:
print (Date_df)
date
0 2017-06-03 00:07:04
1 2017-06-03 00:07:06
2 2017-06-04 00:07:07
3 2017-06-04 00:07:24
4 2017-06-04 00:07:38
dates_day_count = Date_df['date'].dt.date.value_counts().reset_index()
dates_day_count.columns = ['date','counts']
print (dates_day_count)
date counts
0 2017-06-04 3
1 2017-06-03 2
如果需要plotdate
可以使用:
dates_day_count = Date_df['date'].dt.date.value_counts()
dates_day_count.plot.bar()
对于hours
:
dates_day_count = Date_df['date'].dt.hour.value_counts()
dates_day_count.plot.bar()
另外,如果需要一些组合,例如带日期的小时数,可strftime
与http://strftime.org/一起使用:
dates_day_count = Date_df['date'].dt.strftime('%Y-%m-%d %H').value_counts()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句