我正在尝试使用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()
答案 0 :(得分: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