Python 3.x Jupyter笔记本电脑公司;Ipywidgets下拉式级联无法正常工作-可能是我的代码
在Jupyter笔记本中,我试图根据列表框中定义的值,获得3个级联下拉列表框 它的工作方式,但它总是需要2刷新各种下拉列表框,以获得我想要显示的数据 我从中的示例开始,对其进行了修改,添加了第三个下拉框 我无法确定我的更改或观察没有进行动态更新的位置,因此无论我选择什么,当下拉列表框发生更改时,我都会得到新的刷新 感谢所有的帮助 如果代码/调试太多,我表示歉意,但我看到其他人因为没有提供足够的代码/调试而被列入名单 这是代码Python 3.x Jupyter笔记本电脑公司;Ipywidgets下拉式级联无法正常工作-可能是我的代码,python-3.x,jupyter-notebook,ipywidgets,cascadingdropdown,Python 3.x,Jupyter Notebook,Ipywidgets,Cascadingdropdown,在Jupyter笔记本中,我试图根据列表框中定义的值,获得3个级联下拉列表框 它的工作方式,但它总是需要2刷新各种下拉列表框,以获得我想要显示的数据 我从中的示例开始,对其进行了修改,添加了第三个下拉框 我无法确定我的更改或观察没有进行动态更新的位置,因此无论我选择什么,当下拉列表框发生更改时,我都会得到新的刷新 感谢所有的帮助 如果代码/调试太多,我表示歉意,但我看到其他人因为没有提供足够的代码/调试而被列入名单 这是代码 # Modified From: - https://towardsd
# Modified From: - https://towardsdatascience.com/bring-your-jupyter-notebook-to-life-with-
interactive-widgets-bc12e03f0916
import pandas as pd
import numpy as np
import ipywidgets as widgets
from ipywidgets import Layout, AppLayout
from IPython.display import display
# From moriginal article. We use local file
# url = "https://data.london.gov.uk/download/number-international-visitors-london/b1e0f953-4c8a-4b45-95f5-e0d143d5641e/international-visitors-london-raw.csv"
# df_london = pd.read_csv(url)
# Initially, we will get the data and load it into a dataframe:
file = 'C:\\Users\\smelly\\data\\csv\\international-visitors-london-raw.csv'
df_london = pd.read_csv(file, encoding = 'ISO-8859-1')
# Example of when the dropdown year is selected a new text box is
# displayed to show the purpose filter
ALL = 'ALL'
def unique_sorted_values_plus_ALL(array):
unique = array.unique().tolist()
unique.sort()
unique.insert(0, ALL)
return unique
# variable to store the common output for both dropdowns
output = widgets.Output()
# Here are the three dropdowns:
dropdown_year = widgets.Dropdown(description='Year of Visit',
options = unique_sorted_values_plus_ALL(df_london.year))
dropdown_purpose = widgets.Dropdown(description='Purpose of Visit',
options = unique_sorted_values_plus_ALL(df_london.purpose))
dropdown_market = widgets.Dropdown(description='Country Market',
options = unique_sorted_values_plus_ALL(df_london.market))
def common_filtering(year, purpose, market):
output.clear_output()
if (year == ALL) & (purpose == ALL) & (market == ALL):
common_filter = df_london
elif (year == ALL):
common_filter = df_london[df_london.market == market]
elif (year == ALL):
lcommon_filter = df_london[df_london.purpose == purpose]
elif (purpose == ALL):
common_filter = df_london[df_london.year == year]
else:
common_filter = df_london[(df_london.year == year) &
(df_london.purpose == purpose) &
(df_london.market == market)]
with output:
display(common_filter)
# We amend the event handlers to call the lcommon_filtering function and pass the change.new value as well as the
# current value of the other dropdown:
def dropdown_year_eventhandler(change):
common_filtering(change.new, dropdown_purpose.value, dropdown_market.value)
def dropdown_purpose_eventhandler(change):
common_filtering(dropdown_year.value, dropdown_market.value, change.new)
def dropdown_market_eventhandler(change):
common_filtering(dropdown_year.value, dropdown_purpose.value, change.new)
# We bind the handlers to the dropdowns, and that’s it!
dropdown_year.observe(dropdown_year_eventhandler, names='value')
dropdown_purpose.observe(dropdown_purpose_eventhandler, names='value')
dropdown_market.observe(dropdown_market_eventhandler, names='value')
display(dropdown_year)
display(dropdown_purpose)
display(dropdown_market)
display(output)
输出
如果在我的Jupyter笔记本中运行上述操作,我得到的输出如下:
设置2004年的访问年份-数据帧刷新仅显示2004年的条目
Set Year of Visit to 2004 - dataframe refreshes showing only entries for 2004
year quarter market dur_stay mode purpose area Visits (000s) Spend (£m) Nights (000s) sample
6637 2004 January-March Belgium 1-3 nights Air Holiday LONDON 4.443318 1.824694 7.118778 5
6638 2004 January-March Belgium 1-3 nights Air Business LONDON 7.285553 2.124391 11.397926 13
6639 2004 January-March Belgium 1-3 nights Air VFR LONDON 0.502538 0.040518 1.053520 1
6640 2004 January-March Belgium 1-3 nights Air Miscellaneous LONDON 0.380132 0.118981 0.760264 1
6641 2004 January-March Belgium 1-3 nights Sea Holiday LONDON 9.906320 1.340740 29.719000 6
... ... ... ... ... ... ... ... ... ... ... ...
10153 2004 October-December Other Africa 15+ nights Air Holiday LONDON 0.886168 0.429408 11.692750 2
10154 2004 October-December Other Africa 15+ nights Air Business LONDON 0.960389 2.196219 29.084010 3
10155 2004 October-December Other Africa 15+ nights Air VFR LONDON 3.969384 2.211225 221.007949 11
10156 2004 October-December Other Africa 15+ nights Air Study LONDON 1.405100 4.917840 140.509995 1
10157 2004 October-December Other Africa 15+ nights Air Miscellaneous LONDON 0.614155 0.315062 48.518200 1
设定商务访问目的-无任何变化
Set Year of Visit to 2004 - dataframe refreshes showing only entries for 2004
year quarter market dur_stay mode purpose area Visits (000s) Spend (£m) Nights (000s) sample
6637 2004 January-March Belgium 1-3 nights Air Holiday LONDON 4.443318 1.824694 7.118778 5
6638 2004 January-March Belgium 1-3 nights Air Business LONDON 7.285553 2.124391 11.397926 13
6639 2004 January-March Belgium 1-3 nights Air VFR LONDON 0.502538 0.040518 1.053520 1
6640 2004 January-March Belgium 1-3 nights Air Miscellaneous LONDON 0.380132 0.118981 0.760264 1
6641 2004 January-March Belgium 1-3 nights Sea Holiday LONDON 9.906320 1.340740 29.719000 6
... ... ... ... ... ... ... ... ... ... ... ...
10153 2004 October-December Other Africa 15+ nights Air Holiday LONDON 0.886168 0.429408 11.692750 2
10154 2004 October-December Other Africa 15+ nights Air Business LONDON 0.960389 2.196219 29.084010 3
10155 2004 October-December Other Africa 15+ nights Air VFR LONDON 3.969384 2.211225 221.007949 11
10156 2004 October-December Other Africa 15+ nights Air Study LONDON 1.405100 4.917840 140.509995 1
10157 2004 October-December Other Africa 15+ nights Air Miscellaneous LONDON 0.614155 0.315062 48.518200
将国家市场设置为比利时-数据框更新显示2004年,市场=比利时,目的=业务
工作如期进行
Set country Market to Belgium - Dataframe updates showing year 2004, market = Belgium, purpose = Business
year quarter market dur_stay mode purpose area Visits (000s) Spend (£m) Nights (000s) sample
6638 2004 January-March Belgium 1-3 nights Air Business LONDON 7.285553 2.124391 11.397926 13
6642 2004 January-March Belgium 1-3 nights Sea Business LONDON 3.348380 0.558413 6.682670 2
6645 2004 January-March Belgium 1-3 nights Tunnel Business LONDON 8.896553 2.482150 14.327898 20
6649 2004 January-March Belgium 4-7 nights Air Business LONDON 1.873621 1.522606 6.717740 4
6652 2004 January-March Belgium 4-7 nights Sea Business LONDON 1.674190 0.386508 6.682670 1
6654 2004 January-March Belgium 4-7 nights Tunnel Business LONDON 1.161537 0.666272 5.417160 3
6657 2004 January-March Belgium 8-14 nights Tunnel Business LONDON 0.391887 0.589908 5.474880 1
7436 2004 April-June Belgium 1-3 nights Air Business LONDON 12.233637 3.012382 17.830062 13
7440 2004 April-June Belgium 1-3 nights Sea Business LONDON 2.438620 0.174643 7.422190 1
7443 2004 April-June Belgium 1-3 nights Tunnel Business LONDON 9.825394 2.489263 15.252250 21
7446 2004 April-June Belgium 4-7 nights Air Business LONDON 0.779633 0.299457 3.480100 2
7451 2004 April-June Belgium 4-7 nights Tunnel Business LONDON 0.427832 0.547138 1.736200 1
7455 2004 April-June Belgium 8-14 nights Air Business LONDON 0.553725 1.660990 4.364970 1
7458 2004 April-June Belgium 8-14 nights Tunnel Business LONDON 0.445658 0.000000 4.069230 1
7461 2004 April-June Belgium 15+ nights Air Business LONDON 1.714030 0.875407 91.202904 1
8313 2004 July-September Belgium 1-3 nights Air Business LONDON 5.428023 2.023622 9.563622 9
8317 2004 July-September Belgium 1-3 nights Sea Business LONDON 1.936040 0.038780 5.795310 1
8320 2004 July-September Belgium 1-3 nights Tunnel Business LONDON 10.915149 3.652393 16.873318 21
8326 2004 July-September Belgium 4-7 nights Tunnel Business LONDON 2.032828 1.041474 10.141750 3
9291 2004 October-December Belgium 1-3 nights Air Business LONDON 13.759479 5.657460 19.735172 20
9296 2004 October-December Belgium 1-3 nights Sea Business LONDON 2.033270 0.026998 2.033270 1
9299 2004 October-December Belgium 1-3 nights Tunnel Business LONDON 7.496895 2.858201 11.117845 17
9302 2004 October-December Belgium 4-7 nights Air Business LONDON 1.145586 1.520935 4.933070 3
9305 2004 October-December Belgium 4-7 nights Tunnel Business LONDON 0.843375 0.503309 3.373500 2
9308 2004 October-December Belgium 8-14 nights Air Business LONDON 0.321972 0.052481 3.219720 1
9309 2004 October-December Belgium 8-14 nights Tunnel Business LONDON 0.830616 0.246976 5.399000 1
您的过滤器选择逻辑以及事件处理程序代码中出现了一些错误,请参阅下文,了解更清楚地处理输入的不同实现
import functools
def common_filtering(year, purpose, market):
df = df_london.copy()
filters = []
if year is not ALL:
filters.append(df['year']==year)
if purpose is not ALL:
filters.append(df['purpose']==purpose)
if market is not ALL:
filters.append(df['market']==market)
output.clear_output()
with output:
if filters:
df_filter = functools.reduce(lambda x,y: x&y, filters)
display(df.loc[df_filter])
else:
display(df)
def dropdown_year_eventhandler(change):
common_filtering(change.new, dropdown_purpose.value, dropdown_market.value)
def dropdown_purpose_eventhandler(change):
common_filtering(dropdown_year.value, change.new, dropdown_market.value,)
def dropdown_market_eventhandler(change):
common_filtering(dropdown_year.value, dropdown_purpose.value, change.new)
您当然没有太少的代码:)问题是我想复制并粘贴您的代码,并尝试使其正常工作。请在代码中创建一个更简单的数据帧虚拟,而不是从文件加载?例如
data={'First Column Name':['First value','Second value',…],'Second Column Name':['First value','Second value',…],…}df=pd.DataFrame(data,columns=['First Column Name','Second Column Name',…])
@ac24-谢谢您的关注。我已将更新的代码粘贴到pastebin,因为它不允许我在这里执行此操作。非常感谢AC24。我在下拉事件处理程序中看到我的错误。我需要将change.new移到相关的下拉列表中。美好的我认为这只是将df_london数据框复制到df,因此打字更少,因此打字错误的机会也更少。我需要去读一读functools和reduce。至于lambda函数。我见过,从来没用过。再一次谢谢,没问题。reduce只是将未知数量的数据帧过滤器组合成一个数据帧过滤器。