Menu Close

How to append existing Excel sheet with new dataframe using Python Pandas?

Sometimes, we want to append existing Excel sheet with new dataframe using Python Pandas.

In this article, we’ll look at how to append existing Excel sheet with new dataframe using Python Pandas.

How to append existing Excel sheet with new dataframe using Python Pandas?

To append existing Excel sheet with new dataframe using Python Pandas, we can use ExcelWriter.

For instance, we write

import pandas as pd
import openpyxl

workbook = openpyxl.load_workbook("test.xlsx")
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
writer.book = workbook
writer.sheets = dict((ws.title, ws) for ws in workbook.worksheets)
data_df.to_excel(writer, 'Existing_sheetname')
writer.save()
writer.close()

to create an ExcelWriter object with

writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')

We add the workbook with

writer.book = workbook

Then we add the sheets with

writer.sheets = dict((ws.title, ws) for ws in workbook.worksheets)

And then we add the dataframe to the Excel file with

data_df.to_excel(writer, 'Existing_sheetname')

Conclusion

To append existing Excel sheet with new dataframe using Python Pandas, we can use ExcelWriter.

Posted in Python, Python Answers