user3064089
user3064089

Reputation: 65

Writing to Excel Sheet using Pandas

I am trying to update values in an excel sheet using Pandas but the changes overwrites the whole file instead of just a value in in a sheet.

          sheet_1 = pd.read_excel(r"Documents\TestPage.xlsx", 0)
          sheet_1.loc[0, 'Test1'] = 10
          sheet_1.to_excel("Documents\TestPage.xlsx", sheet_name= 'sheet_1')

My code intends to change only Test1 value but instead it overwrites the entire new file losing other pages. I noticed that others facing similar issues but I could figure out looking at the answer.

Appreciate your help and guidance.

Cheer

Upvotes: 0

Views: 57

Answers (1)

Eli Mintz
Eli Mintz

Reputation: 136

You need to use the pandas ExcelWriter class to do what you want.

The specific example from the link above is:

with ExcelWriter('path_to_file.xlsx') as writer:
    df1.to_excel(writer, sheet_name='Sheet1')
    df2.to_excel(writer, sheet_name='Sheet2')

Upvotes: 1

Related Questions