1、将数组写入一个sheet当中
import numpy as np
import pandas as pd
array_1 = np.array([[1, 2], [3, 4]])
data = pd.DataFrame(array_1 )
writer = pd.ExcelWriter('test.xlsx')
data.to_excel(writer, 'sheet_1', float_format='%.2f')
writer.save()
writer.close()
效果:
如果不要行和列的序号
import numpy as np
import pandas as pd
array_1 = np.array([[1, 2], [3, 4]])
data = pd.DataFrame(array_1 )
writer = pd.ExcelWriter('test.xlsx')
# header参数表示列的名称,index表示行的标签
data.to_excel(writer, 'sheet_1', float_format='%.2f', header=False, index=False)
writer.save()
writer.close()
2、将不同数组写入不同的sheet中
import numpy as np
import pandas as pd
writer = pd.ExcelWriter('test.xlsx')
array_1 = np.array([[1, 2], [3, 4]])
array_2 = np.array([[5, 6], [7, 8]])
data_1 = pd.DataFrame(array_1)
data_2 = pd.DataFrame(array_2)
# header参数表示列的名称,index表示行的标签
data_1.to_excel(writer, 'sheet_1', float_format='%.2f', header=False, index=False)
data_2.to_excel(writer, 'sheet_2', float_format='%.2f', header=False, index=False)
# 注意写多个的时候要把所有数据都给了writer后再调用save函数,要不然可能只有一个sheet
writer.save()
writer.close()
3、to_excel的参数
def 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):