gpt4 book ai didi

Python 使用 XlsxWriter 将多个矩阵写入 excel

转载 作者:行者123 更新时间:2023-11-28 22:39:44 26 4
gpt4 key购买 nike

我需要使用 XlsxWriter 在 Excel 中写入多个矩阵。

但是我想提前指定矩阵的位置

这是我的代码

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

CC1 = Get_Data(test1) ## get the corresponding matrix
df = DataFrame(CC) ## put into a dataframe format
df.to_excel(writer, sheet_name="sheet1") ## write into excel

CC2 = Get_Data(test2) ## get the corresponding matrix
df = DataFrame(CC2) ## put into a dataframe format
df.to_excel(writer, sheet_name="sheet1") ## write into excel
writer.save()

如何指定可以插入相应 daraframe 的单元格位置?

最佳答案

要在工作表中移动 DataFrame 的输出,请在调用 to_excel() 时使用命名参数 startrowstartcol。在下面的示例中,输出放在 E3 中左上角的单元格中。

import numpy as np
import pandas as pd
from xlsxwriter.utility import xl_range

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book

df = pd.DataFrame(data=np.random.rand(255))
df.to_excel(
writer, 'TEST',
startcol=4,
startrow=2
)
writer.close()

关于Python 使用 XlsxWriter 将多个矩阵写入 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34417106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com