Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Ipywidgets To Interact With Pandas Dataframe In Jupyter Notebook

Hi I'm trying to get a graph to function with ipywidgets in a Jupyter notebook but am having no such luck. I would like to get the widgets to update based on the dataframe when the update button is functioned. Does anybody know where I am going wrong?

#Import Libraries
import pandas as pd
import folium
import geocoder
import numpy as np
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.offline as py
from ipywidgets import widgets, HBox, Output, interactive

py.offline.init_notebook_mode(connected=False)

#Load Dataset
df = pd.read_excel('LOAS_Biography data (feb 2018).xlsx', sheetname='concerts')

#Rename Columns In DataFrame
df.columns = ['Date', 'City', 'Country', 'Venue', 'Capacity', 'Ticket', 'Tour', 'Event', 'Sold Out', 'Champion']

#Summarises Data by Summing Data Per Country
df = df.groupby('Country', as_index= False).sum()

df = df.head(10)

data = [Bar(x=df.Country,
            y=df.Ticket)]

#Make lists
list1 = list(df['Country'].unique())
#list2 = list(df['City'].unique())

#Create Dropdown Box Widget
w = widgets.Dropdown(
    options= list1,
    value= 'Australia',
    description='Country:',
    disabled=False,
)

#Create Text Box Widget
w1 = widgets.Text(
     value='',
     placeholder='Type something',
     description='Search by:',
     disabled=False
)

#Create Update Button For Widgets
w2 = widgets.Button(description="Update")

def update():
    display(HBox([w, w1, w2]))
    display(py.offline.iplot(data, filename='jupyter-basic_bar'))

def on_button_clicked(b):
    venue = (w.options, w1.value)
    clear_output()
    #print(venue)
    update()

w2.on_click(on_button_clicked)
update()

Thank you

like image 827
Ryan Avatar asked May 11 '18 08:05

Ryan


1 Answers

There is no action in the Update button function. I've added few lines to refresh the dataframe before it call the update function. I'm able to filter the graph after pressing the update update with below code

def on_button_clicked(b):
venue = (w.options, w1.value)    
print(w.value)  
clear_output()  

if w.value=='All':
    df1=df
else:
    df1=df[df['CREATED_YYYYMMDD']==w.value]

update(df1)
like image 152
user10120920 Avatar answered Sep 29 '22 05:09

user10120920