gpt4 book ai didi

python - 返回数据框的按钮

转载 作者:行者123 更新时间:2023-12-04 17:46:37 25 4
gpt4 key购买 nike

我有一个 Jupyter 笔记本,其中包含一个按钮、一些下拉菜单和一个数据框。本质上,选择是在下拉列表中进行的,单击按钮后,会生成一个包含在下拉列表中选择的值的数据框。

我的下拉菜单工作得很好。但是,我的按钮不返回数据框。

这是我的按钮代码:

 def on_button_clicked(b):
# some code that creates from dropdown selections, x.

df = pd.DataFrame(x, index=index,columns=['Pilot', 'Baseline', 'Change'])
return(df)


button.on_click(on_button_clicked)
display(button)

为什么数据框不直接返回?我之前尝试过使用 display(df) ,但每次单击按钮时,每次都会打印一个数据框。

我只想在单击按钮时根据用户选择更改数据框。关于如何处理这个问题的任何提示?谢谢。

最佳答案

已解决。使用 with out: 按钮的输出可以动态变化,即数据框根据用户选择而变化。

在我的例子中,我使用了:

out = widgets.Output()

...


def on_button_clicked(b):
# some code that creates from dropdown selections, x.

df = pd.DataFrame(x, index=index,columns=['Pilot', 'Baseline', 'Change'])
with out:
clear_output(True)
display(df)

button.on_click(on_button_clicked)

with out:
display(df)

out

关于python - 返回数据框的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48227902/

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