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.
CodePudding user response:
assume the vaiable name of DataFrame is df, you can try the following two ways:
transform '2020-01-01:15:00:00 300' to
pd.series
orpd.DataFrame
(e.g. the variable name is data), and then usedf.append(data,ignore_index=True)
.transform '2020-01-01:15:00:00 300' to
pd.series
(e.g. the variable name is data) , and then usedf.loc[len(df)]=data
When there have many lines, the first way is suggested.(more convenient!)
CodePudding user response:
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()