python - 使用破折号下拉列表过滤 pd.DataFrame

标签 python pandas plotly-dash

我是 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 属性。我为下拉列表的 OutputInput 部分添加了一些虚构的值,希望这能给出总体思路。

关于python - 使用破折号下拉列表过滤 pd.DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63983593/

相关文章:

Python:pd.get_dummies() 会丢弃空值

python - 如何减少/缩小 Python Dash Bootstrap 中两列之间的差距

python - 如何删除plotly tools.make_subplots中的绘图边距?

python - 我如何用 <div>$...$</div> 之类的东西替换 LaTeX $...$ 和 $$...$$ 符号?

Python正则表达式;括号内的括号

python - 将整数系列转换为 Pandas 中的时间增量

python - 有没有办法在 Pandas 中将数据类型生成为字典?

python - Plotly:两行子图中的刻度标签问题

完整和部分 IP 的 python IP 验证 REGex 验证

python - Django case when else in filter