Helper function to add a DataFrame to an existing Excel file:
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None, truncate_sheet=False, **to_excel_kwargs): """ Append a DataFrame [df] to existing Excel file [filename] into [sheet_name] Sheet. If [filename] does not exist, then this function will create it. Parameters: filename : File path or existing ExcelWriter (Example: '/path/to/file.xlsx') df : dataframe to save to workbook sheet_name : Name of sheet which will contain DataFrame. (default: 'Sheet1') startrow : upper left cell row to dump data frame. Per default (startrow=None) calculate the last row in the existing DF and write to the next row... truncate_sheet : truncate (remove and recreate) [sheet_name] before writing DataFrame to Excel file to_excel_kwargs : arguments which will be passed to 'DataFrame.to_excel()' [can be dictionary] Returns: None """ from openpyxl import load_workbook import pandas as pd
Examples of using...
Old answer: it allows you to write multiple data frames to a new Excel file.
You can use the openpyxl engine in conjunction with the startrow parameter:
In [48]: writer = pd.ExcelWriter('c:/temp/test.xlsx', engine='openpyxl') In [49]: df.to_excel(writer, index=False) In [50]: df.to_excel(writer, startrow=len(df)+2, index=False) In [51]: writer.save()
with: /temp/test.xlsx:

PS you can also specify header=None if you do not want to duplicate column names ...
UPDATE: you can also check this solution
source share