如何从 ipywidgets 输出返回熊猫数据框
Posted
技术标签:
【中文标题】如何从 ipywidgets 输出返回熊猫数据框【英文标题】:How to return a pandas dataframe from an ipywidgets output 【发布时间】:2021-03-11 10:44:54 【问题描述】:我有一个正在 jupyter 笔记本中处理的数据集,我希望能够将数据作为在 ipywidgets 输出中返回的数据框进行访问。
我尝试了几个选项,但终生无法弄清楚如何访问过滤后的结果。
这是设置过滤器和列表框的代码。包含所有代码的完整笔记本是here
import pandas as pd
import numpy as np
import ipywidgets as widgets
from ipywidgets import Layout, AppLayout
from IPython.display import display
import functools
data = 'year': ['2000', '2000','2000','2000','2001','2001','2001','2001', '2002', '2002', '2002', '2002',
'2003','2003','2003','2003','2004', '2004','2004','2004', '2005', '2005', '2005', '2005',
'2006', '2006', '2006', '2006', '2006', '2007', '2007', '2007', '2007', '2008', '2008', '2008', '2008',
'2009', '2009', '2009', '2009'],
'purpose':['Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business',
'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday',
'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study',
'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday'
],
'market':['Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark',
'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden',
'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France',
'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium',
'Luxembourg', 'France', 'Spain', 'Norway'
]
df_london = pd.DataFrame (data, columns = ['year','purpose', 'market'])
# Get our unique values
ALL = 'ALL'
def unique_sorted_values_plus_ALL(array):
unique = array.unique().tolist()
unique.sort()
unique.insert(0, ALL)
return unique
output = widgets.Output()
# Dropdown listbox
dropdown_year = widgets.Dropdown(description='Year',
options = unique_sorted_values_plus_ALL(df_london.year))
# Function to filter our dropdown listboxe
def common_filtering(year):
df = df_london.copy()
filters = []
# Evaluate our dropdown listbox and return booleans for our selections
if year is not ALL:
filters.append(df['year'] == year)
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_year.observe(dropdown_year_eventhandler, names='value')
ui = widgets.HBox([dropdown_year])
display(ui, output)
我确定它很简单,但我自己无法解决。
问候
汤姆
【问题讨论】:
请问你能把你的代码简化为一个更简单的例子吗?例如从字典和一个过滤器创建一个虚拟数据框。 嘿@ac24 感谢您回复我。我已将问题中的代码更新为带有单个列表框的完整工作示例。填写代码,你应该从 Pastebin 那里得到它吗pastebin.com/gkpHgeQz 【参考方案1】:来自您observe
的函数的代码无法真正以您可以使用的方式返回值。或者,您可以设置一个 global
变量,并将过滤后的数据框分配给该全局变量。我在下面的例子中使用了output_dataframe
。
import pandas as pd
import numpy as np
import ipywidgets as widgets
from ipywidgets import Layout, AppLayout
from IPython.display import display
import functools
data = 'year': ['2000', '2000','2000','2000','2001','2001','2001','2001', '2002', '2002', '2002', '2002',
'2003','2003','2003','2003','2004', '2004','2004','2004', '2005', '2005', '2005', '2005',
'2006', '2006', '2006', '2006', '2006', '2007', '2007', '2007', '2007', '2008', '2008', '2008', '2008',
'2009', '2009', '2009', '2009'],
'purpose':['Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business',
'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday',
'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study',
'Holiday', 'Business', 'VFR', 'Study', 'Holiday', 'Business', 'VFR', 'Study', 'Holiday'
],
'market':['Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark',
'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France', 'Spain', 'Norway', 'Sweden',
'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium', 'Luxembourg', 'France',
'Spain', 'Norway', 'Sweden', 'Germany', 'Austria', 'Denmark', 'Portugal', 'Greece', 'Croatia', 'Belgium',
'Luxembourg', 'France', 'Spain', 'Norway'
]
df_london = pd.DataFrame (data, columns = ['year','purpose', 'market'])
output_dataframe = None
# Get our unique values
ALL = 'ALL'
def unique_sorted_values_plus_ALL(array):
unique = array.unique().tolist()
unique.sort()
unique.insert(0, ALL)
return unique
output = widgets.Output()
# Dropdown listbox
dropdown_year = widgets.Dropdown(description='Year',
options = unique_sorted_values_plus_ALL(df_london.year))
# Function to filter our dropdown listboxe
def common_filtering(year):
global output_dataframe
df = df_london.copy()
filters = []
# Evaluate our dropdown listbox and return booleans for our selections
if year is not ALL:
filters.append(df['year'] == year)
output.clear_output()
with output:
if filters:
df_filter = functools.reduce(lambda x,y: x&y, filters)
output_dataframe = df.loc[df_filter]
else:
output_dataframe = df
display(output_dataframe)
def dropdown_year_eventhandler(change):
common_filtering(change.new)
dropdown_year.observe(dropdown_year_eventhandler, names='value')
ui = widgets.HBox([dropdown_year])
display(ui, output)
【讨论】:
感谢 ac24 - 效果很好。我没有想过使用全局变量并以这种方式将其拉出。以上是关于如何从 ipywidgets 输出返回熊猫数据框的主要内容,如果未能解决你的问题,请参考以下文章