如何将熊猫数据框合并到现有的reportlab表中?

时间:2019-02-14 22:20:18

标签: python pandas reportlab

example_df = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]

我想将example_df pandas数据框集成到现有的Reportlab表中-行数正在变化(示例中可以为3,也可以为20):

rlab_table(['Mean','Max','Min','TestA','TestB'],
['','','','',''], 
['','','','',''], 
['','','','','']])

我尝试过:

np.array(example_df).tolist() 

但我收到此错误(AttributeError:“ int”对象没有属性“ wrapOn”)

我也尝试过:

inputDF = example_df.loc[:,'col1':'col5']
rlab_table(['Mean','Max','Min','TestA','TestB'],
inputDF) 

仅出现数据帧的标题,而不显示其中的数据(即使当我打印inputDF时,我也可以看到列出的所有数据)。

我能够通过以下操作将每一行手动添加到报告实验室表中:

rlab_table(['Mean','Max','Min','TestA','TestB'],
np.array(example_df).tolist()[0],
np.array(example_df).tolist()[1], 
np.array(example_df).tolist()[2]])

但是,问题在于数据框中的行数一直在变化,因此我正在寻找类似于以下的解决方案:

rlab_table(['Mean','Max','Min','TestA','TestB'],
np.array(example_df).tolist()[0:X])] 
#Where X is the number of rows in the dataframe

1 个答案:

答案 0 :(得分:0)

  

rlab_table = [['Mean','Max','Min','TestA','TestB']] + df.values.tolist()

将数据框列表添加到方括号之外(如上面的粗体部分所示)是解决该问题的方法(而且非常快!)