【问题标题】:How to approach dataframe list to html (how to iterate in html code)?如何将数据框列表处理为 html(如何在 html 代码中迭代)?
【发布时间】:2021-12-15 16:02:16
【问题描述】:

我在列表中有多个 pandas 数据框。所以我有几个数据框(df[0],df[1])。我想写入 html 的每个数据框。

python文件中的html代码如下:

html = f'''
        <html>
           <head>
                title>{"test"}</title>
           </head>
           <body>
                 <p>{"just a test sentence"}</p>
           <body>
        <html>
'''

只写一个普通的df很容易({df.head(5).to_html()}):

html = f'''
        <html>
           <head>
                title>{"test"}</title>
           </head>
           <body>
                 <p>{"just a test sentence"}</p>
                 {df.head(5).to_html()}
           <body>
        <html>
'''

在我有 df[0]、df[1] 等的情况下如何处理它。如何遍历 html 中的 df 列表以显示它们中的每一个? 当然我可以使用 {df[0].head(5).to_html()}。但我不知道列表中有多少数据帧,因此我必须使用例如 for 循环。但我不知道如何在 html 代码中插入一个 for 循环。谢谢

【问题讨论】:

  • 显示预期的输出,将尝试找出它和数据格式

标签: python html pandas dataframe


【解决方案1】:

我假设您想将所有 DataFrame 的 HTML 字符串合并为一个。

只需创建一个为给定 DataFrame 生成 HTML 代码的函数

def df_to_html(df):
    return f'''
        <html>
           <head>
                <title>{"test"}</title>
           </head>
           <body>
                 <p>{"just a test sentence"}</p>
                 {df.head(5).to_html()}
           <body>
        <html>'''

然后遍历 DataFrame 列表,在每个上调用函数,最后使用 str.join 连接生成的 HTML 代码。

import numpy as np
import pandas as pd

# Create a list of 3 random DataFrames with shape (10, 3)
# just for the sake of example
df_list = [pd.DataFrame(np.random.randint(10, size=(10,3)), 
                        columns=list("ABC")) 
             for _ in range(3)]

# iterate over the dfs, generate the HTML, and concatenate the results
all_df_html = "".join(df_to_html(df) for df in df_list)

>>> print(all_df_html)

<html>

<head>
  <title>test</title>
</head>

<body>
  <p>just a test sentence</p>
  <table border="1" class="dataframe">
    <thead>
      <tr style="text-align: right;">
        <th></th>
        <th>A</th>
        <th>B</th>
        <th>C</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th>0</th>
        <td>9</td>
        <td>6</td>
        <td>6</td>
      </tr>
      <tr>
        <th>1</th>
        <td>4</td>
        <td>0</td>
        <td>8</td>
      </tr>
      <tr>
        <th>2</th>
        <td>4</td>
        <td>6</td>
        <td>4</td>
      </tr>
      <tr>
        <th>3</th>
        <td>7</td>
        <td>5</td>
        <td>2</td>
      </tr>
      <tr>
        <th>4</th>
        <td>1</td>
        <td>9</td>
        <td>1</td>
      </tr>
    </tbody>
  </table>

  <body>
    <html>
    <html>

    <head>
      <title>test</title>
    </head>

    <body>
      <p>just a test sentence</p>
      <table border="1" class="dataframe">
        <thead>
          <tr style="text-align: right;">
            <th></th>
            <th>A</th>
            <th>B</th>
            <th>C</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th>0</th>
            <td>4</td>
            <td>2</td>
            <td>3</td>
          </tr>
          <tr>
            <th>1</th>
            <td>4</td>
            <td>6</td>
            <td>3</td>
          </tr>
          <tr>
            <th>2</th>
            <td>0</td>
            <td>8</td>
            <td>1</td>
          </tr>
          <tr>
            <th>3</th>
            <td>6</td>
            <td>7</td>
            <td>1</td>
          </tr>
          <tr>
            <th>4</th>
            <td>9</td>
            <td>5</td>
            <td>3</td>
          </tr>
        </tbody>
      </table>

      <body>
        <html>
        <html>

        <head>
          <title>test</title>
        </head>

        <body>
          <p>just a test sentence</p>
          <table border="1" class="dataframe">
            <thead>
              <tr style="text-align: right;">
                <th></th>
                <th>A</th>
                <th>B</th>
                <th>C</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <th>0</th>
                <td>7</td>
                <td>4</td>
                <td>3</td>
              </tr>
              <tr>
                <th>1</th>
                <td>1</td>
                <td>9</td>
                <td>7</td>
              </tr>
              <tr>
                <th>2</th>
                <td>4</td>
                <td>0</td>
                <td>1</td>
              </tr>
              <tr>
                <th>3</th>
                <td>0</td>
                <td>7</td>
                <td>9</td>
              </tr>
              <tr>
                <th>4</th>
                <td>6</td>
                <td>1</td>
                <td>1</td>
              </tr>
            </tbody>
          </table>

          <body>
            <html>

【讨论】:

  • 感谢您的意见。怎么可能只为循环号 1 提供另一种类型的输出?所以我的意思是:第一个循环不使用“只是一个测试句”,而是例如“这是第一句话”。
猜你喜欢
  • 2020-03-05
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 1970-01-01
  • 2020-08-01
  • 2021-07-20
  • 2011-04-04
相关资源
最近更新 更多