将多个数据帧导出到一个 excel 文件?

Exporting multiple dataframes to one excel file?

单击下载按钮后,我尝试将多个破折号数据表下载到不同工作表上的一个 excel 文件中。我有一个回调,它在不同的点击上执行一些计算和 returns 数据帧以破折号数据表。我想获取这些表(在执行计算后)并导出到一个 excel 文件。注意:我无法下载新版本的破折号 dcc.download 无法访问扩展。这就是为什么我试图通过 pandas.

来做到这一点

下面的数据框('Investment_To_Gross_Impact' 等)表示执行计算后的新数据表。

#download to xsxl
@CAD.callback(
    Output('download', 'data'),
    Input('download', 'n_clicks'),
    Input("titleinput", "value"),
    Input('Investment_To_Gross_Impact','data'),
    Input('Investment_From_Gross_Impact','data'),
    Input('Investment_To_Assumptions','data'),
    Input('Investment_From_Assumptions','data'), 
)

def export(n_clicks, value, invto, invfrom, ato, afrom ):
    df1= pd.DataFrame.from_dict(invto)
    df2= pd.DataFrame.from_dict(invfrom)
    df3= pd.DataFrame.from_dict(ato)
    df4= pd.DataFrame.from_dict(afrom)
    if n_clicks>0:
        with pd.ExcelWriter(str(value)+'.xlsx', engine='xlsxwriter') as writer:
            df1.to_excel(writer, sheet_name='Inv To Gross Impact')
            df2.to_excel(writer, sheet_name='Inv From Gross Impact')
            df3.to_excel(writer, sheet_name='Inv To Assumptions')
            df4.to_excel(writer, sheet_name='Inv From Assumptions')
        writer.save()

您可以尝试 pandas merge 将数据帧合二为一,然后导出该数据帧。

没有意识到文件正在下载到我的 py 文件所在的目录中。

def export(n_clicks, netimpact, invto, invfrom, ato, afrom, value):
    netdf= pd.DataFrame.from_dict(data=netimpact)
    df1= pd.DataFrame.from_dict(data=invto)
    df2= pd.DataFrame.from_dict(data=invfrom)
    df3= pd.DataFrame.from_dict(data=ato)
    df4= pd.DataFrame.from_dict(data=afrom)
    filename= (str(value)+'.xlsx')
    writer= pd.ExcelWriter(filename, engine='xlsxwriter')
    netdf.to_excel(writer, sheet_name='Net RAF Impacts', index=False)
    df1.to_excel(writer, sheet_name='Inv To Gross Impact', index=False)
    df2.to_excel(writer, sheet_name='Inv From Gross Impact', index=False)
    df3.to_excel(writer, sheet_name='Inv To Assumptions', index=False)
    df4.to_excel(writer, sheet_name='Inv From Assumptions', index=False)
    writer.save()