【问题标题】:Python write multiple matrix into excel using XlsxWriterPython使用XlsxWriter将多个矩阵写入excel
【发布时间】:2016-03-28 19:00:48
【问题描述】:

我需要使用 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 的单元格的位置?

【问题讨论】:

    标签: python excel xlsxwriter


    【解决方案1】:

    要在工作表中移动 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()
    

    【讨论】:

    • 非常感谢。真的很有帮助。
    【解决方案2】:

    我想分享我能够将矩阵(列表列表)写入 Excel 文件的代码。

    import xlsxwriter
    
    table = [[a, b], [c, d], [e, f, g]] #table must be your matrix 
    
    workbook = xlsxwriter.Workbook('excelFile.xlsx')
    worksheet = workbook.add_worksheet()
    col = 0
    
    for row, data in enumerate(table):
        worksheet.write_row(row, col, data)
    
    workbook.close()
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 2015-02-21
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      相关资源
      最近更新 更多