问题
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