按小时分组并在Bokeh中绘图 [英] Group by hours and plot in Bokeh
问题描述
我试图在Bokeh中获得像股票数据一样的情节,如链接 http://bokeh.pydata.org/en/latest/docs/gallery/stocks.html
2004-01-05,00:00:00,01:00:00,Mon,20504,792
2004-01-05,01:00:00,02:00:00,Mon,16553 ,783
2004-01-05,02:00:00,03:00:00,Mon,18944,790
2004-01-05,03:00:00,04:00:00 ,Mon,17534,750
2004-01-06,00:00:00,01:00:00,Tue,17262,747
2004-01-06,01:00:00,02 :00:00,周二,19072,777
2004-01-06,02:00:00,03:00:00,周二,18275,785
我想使用第2列:startTime和5:count,我想按列 day
分组,在相应的小时内总计计数
。
code:不会输出
import numpy as np
将pandas导入为pd
#from bokeh.layouts从bokeh.plotting导入gridplot
导入图,show,output_file
data = pd.read_csv('one_hour。 csv')
data.column = ['date','startTime','endTime','day','count','unique']
p1 = figure(x_axis_type = '开始时间',y_axis_type = '计数',标题= 每小时计数)
p1.grid.grid_line_alpha = 0.3
p1.xaxis.axis_label = '开始时间'
p1.yaxis。 axis_label ='count'
output_file(count.html,title =time_graph.py)
show(gridplot([[p1]],plot_width = 400,plot_height = 400 ))#打开浏览器
读取列和图并不是问题,而是应用group by和列数据的和操作是我无法执行的操作。
感谢您的帮助,谢谢!
听起来像这样就是你需要的:
data.groupby('startTime')['count']。sum()
输出:
00:00:00 37766
01:00:00 35625
02 :00:00 37219
03:00:00 17534
I am trying to get a plot like a stock data in Bokeh like in the link http://bokeh.pydata.org/en/latest/docs/gallery/stocks.html
2004-01-05,00:00:00,01:00:00,Mon,20504,792
2004-01-05,01:00:00,02:00:00,Mon,16553,783
2004-01-05,02:00:00,03:00:00,Mon,18944,790
2004-01-05,03:00:00,04:00:00,Mon,17534,750
2004-01-06,00:00:00,01:00:00,Tue,17262,747
2004-01-06,01:00:00,02:00:00,Tue,19072,777
2004-01-06,02:00:00,03:00:00,Tue,18275,785
I want to use column 2:startTime and 5:count and I want to group by column day
and sum the counts
in respective hours.
code: Does not give the output
import numpy as np
import pandas as pd
#from bokeh.layouts import gridplot
from bokeh.plotting import figure, show, output_file
data = pd.read_csv('one_hour.csv')
data.column = ['date', 'startTime', 'endTime', 'day', 'count', 'unique']
p1 = figure(x_axis_type='startTime', y_axis_type='count', title="counts per hour")
p1.grid.grid_line_alpha=0.3
p1.xaxis.axis_label = 'startTime'
p1.yaxis.axis_label = 'count'
output_file("count.html", title="time_graph.py")
show(gridplot([[p1]], plot_width=400, plot_height=400)) # open a browser
Reading the column and plot isn't any problem but applying group by and sum operations on the column data is something I am not able to perform.
Appreciate the help, Thanks !
Sounds like this is what you need:
data.groupby('startTime')['count'].sum()
Output:
00:00:00 37766
01:00:00 35625
02:00:00 37219
03:00:00 17534
这篇关于按小时分组并在Bokeh中绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!