我是 Dash 的新手,正在尝试使用下拉列表过滤以下数据框并显示过滤数据框。我无法正确定义回调函数。非常感谢任何示例代码,
df = pd.DataFrame({
'col1': ['A', 'B', 'B', 'A', 'B', 'A'],
'col2': [2, 1, 9, 8, 7, 4],
'col3': [0, 1, 9, 4, 2, 3],
})
最佳答案
Here is the page from the docs我认为这可能是一个很好的起点。如果您提前知道数据框,则可以使用您知道需要的值填充下拉列表。如果不这样做,则需要使用回调来更新下拉 options
属性。
该文档页面上的第一个示例展示了如何从下拉列表中获取值并将其输出。在您的情况下,您可能会使用下拉列表来过滤数据框,例如:
@app.callback(
Output('data-table-id', 'data'),
[Input('dropdown-id', 'value')
)
def callback_func(dropdown_value):
df_filtered = df[df['col1'].eq(dropdown_value)]
return df_filtered.to_dict(orient='records)
然后您将回调的输出设置为 Dash 数据表的 data
属性。我为下拉列表的 Output
和 Input
部分添加了一些虚构的值,希望这能给出总体思路。
关于python - 使用破折号下拉列表过滤 pd.DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63983593/