引言
很多情况下,我们可以将数据结果保存到txt文件中便于后续查看或者再处理,然而为了进行汇报、论文撰写等工作,我们将数据放入表格,为后续整理会提供极大的便利。我们可以利用pandas库进行numpy.ndarray数据保存到excel。
函数说明
DataFrame.
to_excel
(self, 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)[source]Write object to an Excel sheet.
To write a single object to an Excel .xlsx file it is only necessary to specify a target file name. To write to multiple sheets it is necessary to create an ExcelWriter object with a target file name, and specify a sheet in the file to write to.
Multiple sheets may be written to by specifying unique sheet_name. With all data written to the file it is necessary to save the changes. Note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased.
Parameters: |
|
---|
例子
1 import numpy as np 2 import pandas as pd 3 4 data = np.random.randn(2, 3).astype(np.float32) 5 dataFrame = pd.DataFrame(data, 6 index=[‘row 1‘, ‘row 2‘], 7 columns=[‘col 1‘, ‘col 2‘, ‘col 3‘]) # 说明行和列的索引名 8 9 dataFrame2 = dataFrame.copy() 10 11 with pd.ExcelWriter(‘test.xlsx‘) as writer: # 一个excel写入多页数据 12 dataFrame.to_excel(writer, sheet_name=‘page1‘, float_format=‘%.6f‘) 13 dataFrame2.to_excel(writer, sheet_name=‘page2‘, float_format=‘%.6f‘)
结果:
原文:https://www.cnblogs.com/qinduanyinghua/p/12143584.html