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.
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()