【发布时间】:2018-03-10 21:57:59
【问题描述】:
我正在编写一个最终允许使用 ipywidgets 进行数据探索的脚本。我已经为一些可能有兴趣过滤的动态列制作了一些部分,但是以动态方式实现 interact 功能被证明是困难的。下面是我在 Jupyter 中运行的示例代码:
import ipywidgets as widgets
from ipywidgets import interact
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/yankev/testing/master/datasets/nycflights.csv')
df = df.drop(df.columns[[0]], axis=1)
filter_cols = list(['origin','dest','carrier']) #list N columns we want to filter on
filter_df = df[filter_cols] #pull selected N columns from dataframe
filter_df.drop_duplicates(inplace=True) #remove duplicates
#loop through columns and create variables/widgets
for idx, val in enumerate(filter_cols):
#creates N variables (filter0, filter1, filter2) with unique values for each column with an All option
globals()['filter{}'.format(idx)] = ['All']+sorted(filter_df[val].unique().tolist())
#creates N widgets (widget0, widget1, widget2) for interact function below
globals()['widget{}'.format(idx)] = widgets.SelectMultiple(
options=globals()['filter{}'.format(idx)],
value=['All'],
description=val,
disabled=False
)
#looking to make this function dynamic based on the number of columns we want to filter by
#filters down source dataframe based on widget value selections
def viewer(a, b, c = list()):
#if widget selection is 'All', pass the full filter list, else filter only to what is selected in the widget
return df[df['origin'].isin(filter0 if a==('All',) else a)
& df['dest'].isin(filter1 if b==('All',) else b)
& df['carrier'].isin(filter2 if c==('All',) else c)].shape[0]
#displays N filters
#returns record count for filter combination
interact(viewer, a=widget0, b=widget1, c=widget2)
代码的后半部分,在循环之后,是我想要动态化的部分。就目前而言,我必须更改列名标注并为任何其他过滤器添加/删除代码。将操作量限制在脚本中的几个点会很好。
非常感谢任何建议。谢谢!
【问题讨论】:
标签: python pandas interactive ipywidgets