如何使用ipywidgets的交互将数据帧本身作为参数传递给函数但没有固定

我是交互新手,我想通过下拉列表在选项中选择功能名称。这些函数已经定义好了,每个函数都有不同类型的参数,并且大多数都接受一个数据帧作为参数。 我想构建一个结构,用户从列表中选择一个函数,然后该函数的参数也显示为填充。

我很难将数据帧作为参数传递给之前选择的函数,因为数据帧的名称不固定,所以我无法使用固定的小部件。如果我直接分配数据帧,那么显示的数据帧列对我来说也是无用的,因为我希望将整个数据帧传递给函数。 有没有办法通过小部件交互将数据帧作为参数传递给函数?

提前致谢。 问候。

如何使用ipywidgets的交互将数据帧本身作为参数传递给函数但没有固定

functionpicker=widgets.Dropdown(
    options=[
     'func1','func2','func3'],value='func1',style = {'description_width': 'initial'},description='SELECT FUNC:',disabled=False
)

display(functionpicker)

def func1(df,dat1,dat2,dat3):
    print(df)
    print(dat1)
    print(dat2)
    print(dat3)
def run_selected(picked):
    if picked == 'func1' :
        interact(func1,df=df,dat1='dat1',dat2='dat2',dat3='dat3')
 
yuanlei99 回答:如何使用ipywidgets的交互将数据帧本身作为参数传递给函数但没有固定

不确定这是否是您想要的:

functions = ['select func','func1','func2']

x = np.arange(5)
y = np.arange(5)*2
df1 = pd.DataFrame({'x1': x,'y1': y})
df2 = pd.DataFrame({'x2': x,'y2': y})
data = [df1,df2]
def selectfunc(func):
    if func == 'func1':
        def selectdata(data):
            if data == 'df1':
                print(df1)
            elif data == 'df2':
                print(df2)
        wg.interact(selectdata,data=['df1','df2'])
    elif func == 'func2':
        print('this is func2')
    
wg.interact(selectfunc,func=functions)
本文链接:https://www.f2er.com/406.html

大家都在问