Pandas ExcelWriter: how can data be appended to sheets with existing data

我是研究僧i 提交于 2020-06-29 04:02:28

问题


Currently I have a function that writes multiple DataFrames into separate sheets of a new file. However, I am trying to extend the sheets of an existing excel file with data stored in DataFrames.

The function that I use:

sheet_extensions = [df_1, df_2, df_3, df_4]

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

save_xls(list_dfs=sheet_extensions, xls_path='Output.xlsx')

I have read this link that explains how a new sheet can be added to an existing excel file. However, I need to append the DataFrames to existing sheets within an existing excel file. I have found that the to_excel() function has an input variable startrow. I do not have a specific starting row for writing, but a row where the NaN values start.

What modifications of my functions are needed to do this?

来源:https://stackoverflow.com/questions/62326537/pandas-excelwriter-how-can-data-be-appended-to-sheets-with-existing-data

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