Hey everyone,
I have been trying to apply a drop-down widget on a bar chart I made, but I am having trouble applying the filter. I have been using this documentation guide:
Now, I have a dataset setup like this:
id happiness_score time_of_day
01 3 08
02 4 16
03 1 22
04 2 03
05 5 16
I am trying to create a bar graph, with the score as the x-axis, and count of Id as the y-axis, and the time of day as the dropdown widget.
so far I’ve been able to create the widget, which took a bit of time, but here is how I got it:
#make a copy of the original df
df2 = df1
#converting the score to an object
df1['survey_time'] = 1['surveyed_at']= pd.to_datetime(dfnoadmin['surveyed_at'])
df1['survey_time'] = df1.survey_time.map(lambda x: x.strftime('%H'))
df1['score'] = df1['score'].apply(str)
df1.dtypes
#for the widget
dfq = df1
# we will define a function that will handle the input from the dropdown widget
def update_plot(dfz):
noadmin = dfq['score'] == dfz['new']
temp = dfq[noadmin]
data = temp.groupby(dfq['score'])['time_of_day'].count()
x = []
y = []
for i in range(len(data)):
x.append(data.index[i])
y.append(data[i])
graph.restyle({
'x': [x],
'y': [y],
})
graph.relayout({'title': 'blah of {} blah'.format(dfz['new'])})
w = widgets.Dropdown(
options= list(dfq['score'].unique()),
value='0',
description='score',
)
#now for graphing`
#turn the score back to an int for organizational purposes
df2['score'] = df2['score'].apply(int)
fq = df2
fq = fq.groupby(['survey_time']).agg({'id': 'count'})
x= fq['id'].nlargest(400)
data1 = [Bar(
y=x,
x=x.keys(),
text=x,
textposition = 'outside',
marker = dict(
color = 'rgba(158,202,225)',
line=dict(
color='rgb(8,48,107)',
width=1.5),
),
name = "Total Users",
opacity=0.6
)]
layout1 = go.Layout(
title="Time of Day",
xaxis=dict(
title='Surveys Completed by the hour',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
),
yaxis=dict(
title='Time of Day',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
)
#Execute
display(w)
myFigure2 = go.Figure(data = data1 , layout = layout1)
iplot(myFigure2)
However, all it does is ignore the filter, and graphs all the ids across the day, regardless of the score. If I group dataframe fq by surveytime AND score, then count the ids, then nothing appears.
How can I apply the drop down filters to the graph I made?