Group by hours and plot in Bokeh

拜拜、爱过 提交于 2019-12-02 16:58:45

问题


I am trying to get a plot like a stock data in Bokeh like in the link http://docs.bokeh.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 !


回答1:


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


来源:https://stackoverflow.com/questions/38709991/group-by-hours-and-plot-in-bokeh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!