I want to append the contents of a panda dataframe df
to an excelsheet.
This is what I did;
df.to_excel(excel_writer="target.xlsx", sheet_name="sheet_1")
The problem with this code is that it overwrites target.xlsx
. I lost all my old data in target.xlsx
as a result. What I want the code to do is to append, not overwrite the excel sheet.
I am using python 3.7.
with pd.ExcelWriter('target.xlsx', mode='a') as writer:
df.to_excel(writer, sheet_name='sheet_1')
Source: Pandas Dataframe to Excel
I think the easiest way to do this is:
import pandas as pd
import numpy as np
import xlsxwriter
workbook = xlsxwriter.Workbook('arrays.xlsx')
worksheet = workbook.add_worksheet() # a created excel sheet
array = pd.read_csv('array.csv')
array = array.tolist()
row = 0
for col, data in enumerate(array):
worksheet.write_column(row, col, data)
workbook.close()
Here is the xlsxwriter documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With