如何使用dcc.Store()作为数据帧的字典 [英] how to use dcc.Store() for dictionary of dataframe

查看:0
本文介绍了如何使用dcc.Store()作为数据帧的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经挣扎了一天了。有人能帮帮我吗?

我有一本词典来存储大量的数据帧,例如。Dical1={‘key1’:df1,‘key2’:df2,‘key3’:df3}

现在我使用dcc.Store()在回调之间共享这些词典数据。

首先,我使用json.ump(Didic1)将字典存储到dcc.Store()中,然后我得到了一个类型错误:DataFrame类型的对象不是JSON可序列化的。

那么如何将数据帧的字典存储到dcc.Store()中呢?在将所有数据帧放入词典之前,我必须转换它们吗?它会重新运行我所有的代码。是否有可能在不将每个数据帧转换为json的情况下执行此操作,而只处理字典?

谢谢你的帮助。我真的很感激。


@app.callback(
    [
    Output('stored-shared-data-time-dict','data'),
    [
    Input('load-area-data','n_clicks'),
    ],

    
    prevent_initial_call=True,   # disable output in the first load
)
def change_area_data(n_clicks):  #radio_value,range_slider_values
    
    s1=json.dumps(dict1) 
    return s1 

然后,我从dcc.Store()加载词典。


@app.callback(
    [
        Output("timeplot-graph", "figure"),
        Output("timeplot-markdown", "children"),
    ],
    [
        Input("plot-well-prod", "n_clicks"),
    ],
    [
        State('stored-shared-data-time-dict','data'),
    ],
    
    prevent_initial_call=True,   # disable output in the first load
)

def change_well_time_graphs(n_clicks,well_cell, well_data,json_dict):
    

    df = json.loads(json_dict)
   
     
    (fig,markdown_text)=make_graph(df)

    print('Done Creating Graphes')
        
    return (fig,markdown_text)

JSON

错误TypeError: Object of type DataFrame is not JSON serializable意味着您需要将数据帧转换为与推荐答案兼容的内容(例如字典)。然后,您需要向dcc.Store()传递该json可序列化对象的元组或列表(例如,dict的列表)。一旦希望在另一个回调中读取该数据帧,只需将dcc.Store对象转换回数据帧即可。以下是如何在代码中执行此操作的示例:


@app.callback(
    [Output('stored-shared-data-time-dict','data')],
    [Input('load-area-data','n_clicks')]
    , prevent_initial_call=True,   # disable output in the first load

)
def change_area_data(n_clicks):  #radio_value,range_slider_values
    s1 = [df.to_dict()]
    return s1 


@app.callback(
    [
        Output("timeplot-graph", "figure"),
        Output("timeplot-markdown", "children"),
    ],
    [
        Input("plot-well-prod", "n_clicks"),
    ],
    [
        State('stored-shared-data-time-dict','data'),
    ], prevent_initial_call=True,   # disable output in the first load
)
def change_well_time_graphs(n_clicks,well_cell, well_data,json_dict):
    df = pd.DataFrame.from_dict(json_dict, orient='index')   
    (fig,markdown_text)=make_graph(df)
    print('Done Creating Graphes')
        
    return (fig,markdown_text)

编辑: 在您希望存储在dcc.Store中的数据帧字典的情况下,同样的方法也适用,但方法是对字典中的每个数据帧执行此操作,然后将字典发送到存储,然后迭代回调中的每个字典并将其转换回数据帧,以下是代码的示例:

@app.callback(
    [Output('stored-shared-data-time-dict','data')],
    [Input('load-area-data','n_clicks')]
    , prevent_initial_call=True,   # disable output in the first load

)
def change_area_data(n_clicks):  #radio_value,range_slider_values
    temp_dict = dict1
    for key, df in temp_dict.items():
        temp_dict[key] = df.to_dict()
    return [temp_dict]


@app.callback(
    [
        Output("timeplot-graph", "figure"),
        Output("timeplot-markdown", "children"),
    ],
    [
        Input("plot-well-prod", "n_clicks"),
    ],
    [
        State('stored-shared-data-time-dict','data'),
    ], prevent_initial_call=True,   # disable output in the first load
)
def change_well_time_graphs(n_clicks,well_cell, well_data,json_dict):
    df_list = []
    for _, dic in json_dict.items():
        df_list.append(pd.DataFrame.from_dict(dic, orient='index')) 
    # the rest of your logic using the list of df's "df_list"
    .
    .
    .
    return (fig,markdown_text)

这篇关于如何使用dcc.Store()作为数据帧的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆