Reputation: 31
I'm making histograms of grades historically by term.
I want to make an interactive Bokeh bar chart with a slider that can cycle through the terms.
I have the bar chart working on a single term but when I try to add additional terms I can't get the bar chart to select a single term and then do the updating and slight through it.
I really just need to have some help getting the groupby object to select just one term.
import os
from bokeh.plotting import figure, ColumnDataSource
from bokeh.io import curdoc, output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import Slider
from bokeh.transform import jitter
from bokeh.palettes import viridis
from bokeh.transform import factor_cmap
Importing necessary modules.
input_file = 'Alltime_grades.csv'
output_file('Grades.html')
df = pd.read_csv(input_file)
group = df.groupby(['Term','Grade'])
Here the is the input file import code. The input file has 3 columns - "Term", "Grade", and "Grade_Count".
So say Spring 2019 - A - 5000
I got this to work on a single term by not grouping by term even tho the field was still there.
Grades = ['A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'F']
Above, I made a list of grades so that they display in the correct order
source = ColumnDataSource(group)
grade_cmap = factor_cmap('Grade',palette=viridis(22) , factors=Grades)
###
p = figure(plot_height=500, plot_width=700, title='Grades Over Time', toolbar_location=None, tools="", x_range = Grades)
###
p.vbar(x='Grade', top='grade_count_max', width=.75, bottom=0, source=source, line_color=grade_cmap, fill_color=grade_cmap)
p.y_range.start = 0
p.xaxis.axis_label = 'Grade'
p.yaxis.axis_label = 'Count of Grade'
curdoc().add_root(p)
show(p)
Above is the code I used to make the single bar chart to display.
source = ColumnDataSource(data={
'x' : group.loc['Spring 2019'].Grade,
'top' : group.loc['Spring 2019'].grade_count_max)})
For the multiple terms I tried this as the Column Data Source.
So for now I am getting can't use 'loc' method on Group by.
I need to have some way to make it select a single term so that I can them make an update function that follows the slider and updates the terms.
Beyond that I'm not even sure that I can cycle through non-numeric values on a slider but a good first step would be to be able to slice the group by at all.
Thanks for any help you can provide.
Upvotes: 0
Views: 116
Reputation: 31
I finally figured this out by using the .get_group method on the GroupBy Object!
Upvotes: 0
Reputation: 212
I think you should describe your data while is not clear what is the main problem. This is a problem with pandas and not with bokeh, however, I ran with a similar problem but I cannot understand your data problem. The way I solved (in my case) was to use the pandas function Grouper and then I applied the mean. The ColumnDataSource for instance, allowed me to use the .loc function.
Upvotes: 0