0

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

1 Answer 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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.