Issue
I have a df composed of eight columns. I am interested in two of them: "Id" and "Description". "Id" contains a numerical value while "Description" contains string values (e.g. "high emergency", "low emergency", "zone 1"). I would like to filter the df using a drop-down menu containing a selected list of words. For example, having the words "emergency" selected in the drop-down menu the result should be a new df filtered with only the rows that in the column "Description" contain the word "emergency".
Once filtered this, I would like to plot the number of occurrences of the remaining Ids (counted with value_counts() method) in a bar chart that updates depending on the selected keyword in the drop-down menu.
I report here the code I wrote until now (df is already imported):
import ipywidgets
from bokeh.io import push_notebook
from bokeh.plotting import figure
from bokeh.io import output_notebook, show, reset_output
from bokeh.models import Range1d
output_notebook()
# drop-down widget
drop_down = ipywidgets.Dropdown(options=['emergency',
'zone'],
description='Keyword:'
)
#data. I have problems here because I am passing alarm_count that is defined later in the function
x_bar_data_ipyw = alarm_count.IdAlarm.astype(str)
y_bar_data_ipyw = alarm_count.Count
# figure and plot
bar_chart_interactive = figure(x_range=x_bar_data_ipyw, plot_height=300)
bar_ipyw = bar_chart_interactive.vbar(x_bar_data_ipyw, top=y_bar_data_ipyw, color='green', width=0.5)
bar_chart_interactive.y_range=Range1d(0, 18)
# function - bar chart
def filter(word):
if word == 'emergency':
alarm_count = []
alarm_count = df[df.Description.str.contains(word)].IdAlarm.value_counts().reset_index()
alarm_count.columns = ['IdAlarm','Count']
#alarm_count.sort_values(by = "Count", ascending = False)
elif word == 'zone':
alarm_count = df[df.Description.str.contains(word)].IdAlarm.value_counts().reset_index()
alarm_count.columns = ['IdAlarm','Count']
#alarm_count.sort_values(by = "Count", ascending = False)
push_notebook()
show(bar_chart_interactive, notebook_handle=True)
# interaction
ipywidgets.interact(filter, word=drop_down)
As of now, I a not able to plot the filtered graph and, then, I am not able to update the plotted graph. Any suggestion?
EDIT:
A sample of my df:
Solution
For this you need to run the ipywidget in one cell, then capture the value.In the next cell you can apply the filter to the df based on selection and do the plot. use observe to capture the widget selection value. use global or self variable to store value.
#first cell
drop_down = ipywidgets.Dropdown(options=['emergency',
'zone'],
description='Keyword:'
)
def observe_type_value(type_value):
global type_selection
type_selection = type_value['new']
print(type_selection)
drop_down.observe(observe_type_value, 'value')
display(drop_down)
#second cell
filter(type_selection)
#now the df is filtered.Use this df for plotting
Answered By - Thomas Manjooran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.