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表现出色,现在我想在每张表的最后一行的正下方(每行之间留出一行)直接在每张表中添加以下内容:
a=10
b=20
c=40
d=80
我该怎么做?
答案 0 :(得分:0)
这是一种实现方法:
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文档的Working with Pandas chapter。