【问题标题】:How to load a Pandas DataFrame according to a Dash Callback?如何根据 Dash 回调加载 Pandas DataFrame?
【发布时间】:2020-12-21 19:19:44
【问题描述】:

我有以下函数将下拉选择与文件路径相关联:

def origin_func():

return html.Div(                                                                  
    children=[                                                                    
        dcc.Markdown('Select country'),                              
        dcc.Dropdown(                                                             
            id='origin_div',                                                      
            options=[{'label': 'Malta', 'value': './data/malta.csv'}, {'label': 'Japan', 'value': './data/japan.csv'}],
            value='./data/malta.csv'                                   
        )                                                                         
    ]                                                                             
)

通过回调,我可以将这个选择变成 Pandas DataFrame:

@app.callback(                          
    Output('country_table', 'children'),
    Input('origin_div', 'value')        
)                                       
def countrytable(origin_div):           
    df = pd.read_csv(origin-div)        
    return df                           

问题是,我如何将存储在 Dash 变量 ('country_table') 中的 Pandas DataFrame 传递到布局函数中,例如:

    def destination_func():
            children=[
                dcc.Markdown('Select country of destination'),
                dcc.Dropdown(
                    id='destination_div',
                    options=[{'label': i, 'value': i} for i in df['Destination'].drop_duplicates()],
                    value=df['Destination'].drop_duplicates()[0]
                )
            ]
        )

【问题讨论】:

    标签: python html plotly-dash


    【解决方案1】:

    我认为有两种主要方法可以做到这一点。

    1. countrytable 回调函数的输出设为您希望数据帧结束的位置。

    2. 将数据框传递给其他东西以保存它,并在以后需要时从该位置检索它。

    看起来您正在这里执行第二个选项。您可以使用

    输出数据框

    df.to_dict(orient='records')

    然后,当您以后需要时,将其读回并执行

    df = pandas.DataFrame.from_dict(dict_arg)

    读取dict 以转换为数据帧的函数会将第一个回调中的Output 作为InputState 参数。

    编辑:

    您需要为第二个选项添加的回调如下所示:

    @app.callback(                          
        Output('whatever-component-in-your-layout', 'whatever-prop'),
        Input('country_table', 'children')        
    )                                       
    def countrytable(stored_df):           
        df = pd.DataFrame.from_dict(stored_df)
        return df      
    

    【讨论】:

    • 嗨@coralvanda,因为 2d 方式不起作用 - 无论我输入为 'dict_arg' 我都有以下错误:“NameError: name 'dict_arg' is not defined” - 你会怎么做做第一个选项?
    • 添加了一个例子。如果您还需要帮助,请告诉我。
    • 对我不起作用,我有以下错误:“NameError: name 'df' is not defined”。我正在尝试做的想法是能够选择一个 csv 并通过 Dash 中的下拉列表将其作为 DataFrame 加载。一个完整的最小示例将是一个很大的帮助,因为我看到网络上的许多人都在为此苦苦挣扎,却没有找到解决方案。
    • 你能分享完整的回溯吗?
    • 通过尝试提供一个最小示例来指出我在这个问题上面临的问题,我想到了一个令人满意的解决方案来解决它。不幸的是,它带来了我制定的另一个问题here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2018-08-13
    • 2020-04-16
    • 1970-01-01
    • 2018-04-22
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多