'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 1:[1]

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])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 prahasanam_boi