How To Append Dataframe To Existing Excel File In Python

Appending data to an existing Excel file is a common task in data analysis. In Python, it can be easily done with the help of some libraries.

Here’s a tutorial on how to append a dataframe to an existing Excel file in Python:

Example Files:

Excel

CSV

Column 1,Column 2,Column 3
Value 10,Value 11,Value 12
Value 13,Value 14,Value 15
Value 16,Value 17,Value 18

Step 1: Import the Required Libraries

To start, we need to import the libraries we need. We will be using the pandas and openpyxl libraries for this task:

Step 2: Load the Existing Excel File

Next, we need to load the existing Excel file to which we want to append data. We can do this using the Load Workbook function of the openpyxl library as shown below:

This code will load the existing Excel file and prepare it for writing.

Step 3: Load the DataFrame to be Appended

We also need to load the DataFrame that we want to append to the existing Excel file. We can do this using the pandas library:

This code will load the DataFrame that we want to append.

Step 4: Append the DataFrame to the Existing Excel File

Finally, we can append the DataFrame to the existing Excel file using the pandas to_excel function:

This code will add the DataFrame to the ‘Sheet1’ sheet of the existing Excel file and save the changes.

Conclusion

Appending a DataFrame to an existing Excel file in Python is a simple task that can be done using the libraries pandas and openpyxl. By following the steps outlined above, you should be able to easily append data to an existing Excel file.

Here’s the full code:

Output: