-1
import pandas as pd
df1 = pd.DataFrame(Numpy_Array1)
df2 = pd.DataFrame(Numpy_Array2)
writer = pd.ExcelWriter('Results.xlsx', engine='xlsxwriter')
df1.to_excel(writer, sheet_name = 'Array1')
df2.to_excel(writer, sheet_name = 'Array2')
writer.save()

您好,我已经使用 ExcelWriter 编写了一些数据框来 excel,现在我想在每张表的最后一行正下方添加以下内容(中间留出一行):

a=10
b=20
c=40
d=80 

我怎么做 ?

4

1 回答 1

1

这是一种方法:

import pandas as pd

Numpy_Array1 = [10, 20, 30, 20, 15]
Numpy_Array2 = [10, 20, 30, 20, 15, 30, 45]

df1 = pd.DataFrame(Numpy_Array1)
df2 = pd.DataFrame(Numpy_Array2)

writer = pd.ExcelWriter('Results.xlsx', engine='xlsxwriter')

df1.to_excel(writer, sheet_name = 'Array1')
df2.to_excel(writer, sheet_name = 'Array2')


workbook = writer.book

worksheet = writer.sheets['Array1']
last_row = len(Numpy_Array1) + 2  # Allow for header row and padding.
col = 0
worksheet.write(last_row + 0, col, 'a')
worksheet.write(last_row + 1, col, 'b')
worksheet.write(last_row + 2, col, 'c')
worksheet.write(last_row + 3, col, 'd')

col = 1
worksheet.write(last_row + 0, col, 10)
worksheet.write(last_row + 1, col, 20)
worksheet.write(last_row + 2, col, 40)
worksheet.write(last_row + 3, col, 80)

# Repeat for Numpy_Array2 or add a loop.

writer.save()

输出:

在此处输入图像描述

解决方案的关键是获取底层工作簿的句柄并从那里获取工作表。有关更多信息,请参阅XlsxWriter 文档的使用 Pandas 章节

于 2019-04-11T15:22:37.493 回答