我正在尝试按照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()