pandas玩转Excel及数据分析(三) Excel文件写入

2021-03-01  本文已影响0人  熊爸天下_56c7

一. 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的作用:

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")

三. 读写CSV

上一篇下一篇

猜你喜欢

热点阅读