0

I want to save the latest data to excel For example:

 df.to_excel('Excel A')


I have an excel file like this: Excel A
  Datetime                Cash
  2020-01-01:13:00:00      100
  2020-01-01:14:00:00      200


When I got the latest data like this:(data)
 2020-01-01:15:00:00    300

How can I save the latest data to Excel A

thanks for answering.

Czz
  • 1
  • 3
  • Do you mean, to _**append**_ to the existing data in excel? – Vladimir Fokow Aug 30 '22 at 01:50
  • Does this answer your question? [Append existing excel sheet with new dataframe using python pandas](https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas) – Vladimir Fokow Aug 30 '22 at 01:54

2 Answers2

0

assume the vaiable name of DataFrame is df, you can try the following two ways:

  1. transform '2020-01-01:15:00:00 300' to pd.series or pd.DataFrame(e.g. the variable name is data), and then use df.append(data,ignore_index=True).

  2. transform '2020-01-01:15:00:00 300' to pd.series(e.g. the variable name is data) , and then use df.loc[len(df)]=data

When there have many lines, the first way is suggested.(more convenient!)

0
import pandas as pd
from openpyxl import load_workbook
 
ExcelWorkbook = load_workbook("path/to/Excel A")
writer = pd.ExcelWriter("path/to/Excel A", engine='openpyxl')
writer.book = ExcelWorkbook
df_data.to_excel(writer) #pandas supports appending dataframe to existing excel file
writer.save()
writer.close()