How does pandas write data completely in the sheet of an existing excel? How does pandas delete a row of data in excel?

when using pandas for file writing, if the original sheet already has data, the newly written data is overwritten on the original data without deletion. For example, there are 4 rows of data originally, and I want to delete one row. After read is dataframe, drop a row and get 3 new rows of data. After to_excel again, only the first 3 rows are covered, and the fourth row in the table is still there.
how can I really delete a row in excel so that the number of rows is-1?
I delete line 0 and write line 123, which turns out to be line 3.
clipboard.png

the following is my code (see "how to write to one sheet without affecting other sheeets" on stackoverflow.

-sharp drop
data = pd.read_excel("data.xlsx", sheetname=sheet_name)
mydata = data.drop([0], axis=0)
-sharp 
book = load_workbook("data.xlsx")
writer = pd.ExcelWriter("data.xlsx",engine="openpyxl")
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
mydata.to_excel(writer, "mysheet")
writer.save()

if you want to overwrite all the data, you can erase all the original data, and then write the new data.
specific code:

  

Thank you, boss. It's useful. But I currently want to rewrite several sheet in an excel. Could you ask me. How can I optimize this code when I need to overwrite some of the sheet, but not all of them?

Menu