让 Ipywidgets 与 Jupyter Notebook 中的 Pandas Dataframe 交互
Getting Ipywidgets To Interact With Pandas Dataframe In Jupyter Notebook
您好,我正在尝试在 Jupyter 笔记本中使用 ipywidgets 制作图表,但我没有这样的运气。当更新按钮起作用时,我想让小部件根据数据框进行更新。有人知道我哪里错了吗?
#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()
谢谢
“更新”按钮功能中没有任何操作。在调用更新函数之前,我添加了几行来刷新数据框。使用以下代码
按更新更新后,我可以过滤图表
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)
您好,我正在尝试在 Jupyter 笔记本中使用 ipywidgets 制作图表,但我没有这样的运气。当更新按钮起作用时,我想让小部件根据数据框进行更新。有人知道我哪里错了吗?
#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()
谢谢
“更新”按钮功能中没有任何操作。在调用更新函数之前,我添加了几行来刷新数据框。使用以下代码
按更新更新后,我可以过滤图表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)