Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I stop pandas overwriting old data in Excel file?

I have a dataframe that I’m creating within Python and want this dataframe to be appended to the end of an existing file every day. I currently have the code below but this overwrites any data on the existing sheet, despite me specifying the mode as append. How can I modify this so that the existing data is not modified, only new data is added to the end.

with pd.ExcelWriter(r'C:\Users\XXX\Downloads\Digitalisation\mat_flow\reblend.xlsx', engine="openpyxl", mode="a") as writer:
    df.to_excel(writer, sheet_name = ft_tags_final[i][j])

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

please try this:

append new data to existing data before writing

df_old = pd.read_excel(r'C:\Users\XXX\Downloads\Digitalisation\mat_flow\reblend.xlsx',sheet_name = ft_tags_final[i][j])
df = df_old.append(df)
with pd.ExcelWriter(r'C:\Users\XXX\Downloads\Digitalisation\mat_flow\reblend.xlsx', engine="openpyxl", mode="a") as writer:
    df.to_excel(writer, sheet_name = ft_tags_final[i][j])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading