gpt4 book ai didi

python-3.x - 如何从 ipywidgets 输出返回 pandas 数据帧

转载 作者:行者123 更新时间:2023-12-03 08:31:04 25 4
gpt4 key购买 nike

我正在 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)

我确信这是一个简单的问题,但我自己无法解决。

问候

汤姆

最佳答案

观察的函数代码无法真正以您可以使用的方式返回值。或者,您可以设置一个全局变量,并将过滤后的数据帧分配给该全局变量。我在下面的示例中使用了 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)

关于python-3.x - 如何从 ipywidgets 输出返回 pandas 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65057860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com