【问题标题】:dynamically generate html tables with row spans/nests动态生成具有行跨度/嵌套的 html 表
【发布时间】:2019-04-29 10:37:17
【问题描述】:

我想从 Python 数据帧创建 HTML 表格以生成电子邮件报告。

我希望能够通过设置不同的行跨度以在不同的列上进行聚合来动态生成这些表并进行分组。用户可以在其中选择他们想要对数据进行分组的列。

按一列分组时,我可以很容易地做到这一点。但是说当我想在两列上执行此操作时,例如下面的 Code 和 CCY 列:

https://imgur.com/kgwdBFm

<html>
   <table cellpadding="4" style="border: 1px solid #000000; border-collapse: collapse;" border="1">
      <tr>
      <thead>
         <th>Code</th>
         <th>Total # of Entries</th>
         <th>A</th>
         <th>B</th>
         <th>Description</th>
         <th>Ccy</th>
         <th>Date 1</th>
         <th>Date 2</th>
         <th>Px_chg_pct</th>
      </thead>
      </tr>
      <tr>
         <td>ABC</td>
         <td>1</td>
         <td>4405</td>
         <td>3B1070</td>
         <td>TEXT</td>
         <td>USD</td>
         <td>1109</td>
         <td>1108</td>
         <td>0.2415</td>
      </tr>
      <tr>
         <td rowspan=3>DEF</td>
         <td rowspan='3'>3</td>
         <td>6490</td>
         <td>17878</td>
         <td>CURRENCY EXCHANGE</td>
         <td rowspan=2>CAD</td>
         <td>11.6</td>
         <td>7.9</td>
         <td>-0.5</td>
      </tr>
      <tr>
         <td colspan>C036490</td>
         <td>U78</td>
         <td>CURRENCY EXCHANGE1</td>
         <td>20.57</td>
         <td>27.9</td>
         <td>-0.2625</td>
      </tr>
      <tr>
         <td colspan>C036490</td>
         <td>31B3078</td>
         <td>CURRENCY EXCHANGE</td>[enter image description here][1]
         <td>USD</td>
         <td>0.57</td>
         <td>2.1</td>
         <td>-0.25</td>
      </tr>
   </table>
</html>

我在执行此操作时遇到了一些问题。

我目前的逻辑是定位聚合器列并在聚合器索引之后创建一个总列,然后对其进行“硬编码”。请原谅我画得很快的质量。

def add_data_rows_to_tables(html_code,pivot_pos,length,style,aggregate_on):
if length == 1:
    html_code += row_start + style + '>'
    for index, row in pivot_pos.iterrows():
        for all in pivot_pos.columns.tolist():
            html_code += '<td>%s</td>' % row[all]
    html_code += row_end

else:
    html_code += row_start + style + '>'
    first = pivot_pos.iloc[0]
    for all in pivot_pos.columns.tolist():
        if all != aggregate_on and all != 'Total for %s'%aggregate_on:
            html_code += '<td>%s</td>' % first[all]
        if all == aggregate_on:
            html_code += '<td rowspan=%d>%s</td>' % (length, pivot_pos[aggregate_on].iloc[0])
        if all == 'Total for %s'%aggregate_on:
            html_code += '<td rowspan=%d>%s</td>' % (length, length)
        if all == 'Ccy':
            html_code += '<td rowspan=%d>%s</td>' % (length, length)

    html_code += row_end

    for i in range(1,length):
        html_code += row_start + style + '>'
        for all in pivot_pos.columns.tolist():
            if all != aggregate_on and all != 'Total for %s'%aggregate_on:
                html_code += '<td>%s</td>' % pivot_pos[all].iloc[i]
        html_code += row_end
return html_code

但是,当添加多个列以聚合/跨度时,这并不能很好地工作,而且我无法进行正确的递归调用,因为必须提前设置跨度(或者我可能误解了)。

如果你们对此有任何提示,请告诉我。 提前致谢!

【问题讨论】:

  • 您是否尝试过使用熊猫的to_html 功能,并解释为什么它不适合您的目的。 pandas.pydata.org/pandas-docs/stable/generated/…
  • 在尝试嵌套和聚合特定列时,我似乎找不到太多关于使用 to_html 的资源。我希望能够指定包装和行/列跨度

标签: python html dataframe html-table


【解决方案1】:

我想我将尝试做的是将可能的嵌套及其相应的行跨值预处理/预计算到每个类别组合的矩阵中。 然后在创建表时,查找每行数据的行跨度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-30
    • 2017-09-10
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多