一. to_excel函数
pandas.DataFrame.to_excel
DataFrame.to_excel(excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None, storage_options=None)
主要参数:
import pandas as pd
from datetime import datetime
df1 = pd.DataFrame({
'date': [datetime(2020, 1, 1), datetime(2020, 1, 2)],
'sales': [10, 20]
})
print(df1)
df1.to_excel('tb.xls', sheet_name="JUN")
二. ExcelWriter
pandas.ExcelWriter
class pandas.ExcelWriter(path,date_format=None,datetime_format=None, mode='w')
ExcelWriter的作用:
- 输出多个sheet
- 设置datetime格式等
import pandas as pd
from datetime import datetime
df1 = pd.DataFrame({
'date':[datetime(2020,1,1),datetime(2020,1,2)],
'sales':[10,20]
})
df2 = pd.DataFrame({
'date':[datetime(2020,2,1),datetime(2020,2,2)],
'sales':[15,25]
})
print(df1)
print(df2)
with pd.ExcelWriter('tb.xls',datetime_format="YYYY-MM-DD") as writer:
df1.to_excel(writer,sheet_name= "JUN")
df2.to_excel(writer,sheet_name= "FEB")
网友评论