Save list of DataFrames to multisheet Excel spreadsheet

隐身守侯 提交于 2019-11-26 18:24:40

You should be using pandas own ExcelWriter class:

from pandas import ExcelWriter
# from pandas.io.parsers import ExcelWriter

Then the save_xls function works as expected:

def save_xls(list_dfs, xls_path):
    with ExcelWriter(xls_path) as writer:
        for n, df in enumerate(list_dfs):
            df.to_excel(writer,'sheet%s' % n)
        writer.save()

In case anyone needs an example of how to do this with a dictionary of dataframes:

from pandas import ExcelWriter

def save_xls(dict_df, path):
"""
Save a dictionary of dataframes to an excel file, with each dataframe as a seperate page
"""

writer = ExcelWriter(path)
for key in dict_df:
    dict_df[key].to_excel(writer, key)

writer.save()

example: save_xls(dict_df = my_dict, path = '~/my_path.xls')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!