0

我正在尝试按照https://stackoverflow.com/a/17652442/2478647中的模式绘制 Reportlab 中 Pandas pivot_table 的输出。

import pandas as pd
from reportlab.pdfgen import canvas
from reportlab.platypus import SimpleDocTemplate, Table, Paragraph
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, legal, portrait, landscape
from reportlab.lib.styles import getSampleStyleSheet

df = pd.DataFrame(randn(8, 2), columns=['var A', 'var B'])
df['year'] = ['2013','2013','2013','2013','2014','2014','2014','2014']
df['run'] = ['base','base','option','option','base','base','option','option']
df['id'] = [1,2,1,2,1,2,1,2]

df.pivoted = pd.pivot_table(df, values=['var A','var B'], rows=['id'], cols=['year','run'], aggfunc='sum')

doc = SimpleDocTemplate('temp.pdf', pagesize=landscape(letter), showBoundary=0, 
                            topMargin=72*.75,
                            bottomMargin=72*1,
                            leftMargin=72*.5,
                            rightMargin=72*.5)

lista = [df.pivoted.columns[:,].values.astype(str).tolist()] + df.pivoted.values.tolist()

elements = []
table = Table(lista, repeatRows=3) # repeat the header rows
elements.append(table)    
doc.build(elements)

由于多个列标签,我在“lista = ...”行收到此错误:

ValueError: cannot set an array element with a sequence

如何构建代码以使 pivot_table 列与 reportlab 配合得很好?或者,您对使用 pivot_table 输出编写 PDF 报告的不同方法有什么建议吗?

编辑:我对这个修改非常接近,但仍然不保留 y 轴标签

lista = map(list, zip(*df.pivoted.columns.values)) + df.pivoted.values.tolist()
4

1 回答 1

1

这个函数非常接近——它返回一个输入到reportlab表的列表和要重复的表标题行数。出于某种原因,它不适用于简单的表格——那些只有一个标题行的表格。

def prepare_df_for_reportlab(df):
    df2 = df.reset_index() # reset the index so row labels show up in the reportlab table
    n = df2.columns.nlevels # number of table header rows to repeat
    if n > 1:
        labels = map(list, zip(*df2.columns.values))
    else:
        labels = [df2.columns[:,].values.astype(str).tolist()]
    values = df2.values.tolist()
    datalist = labels + values
    return datalist, n
于 2013-09-13T19:04:33.710 回答