【问题标题】:Formatting HTML pandas tables in python在 python 中格式化 HTML pandas 表
【发布时间】:2020-08-19 12:34:22
【问题描述】:

我正在使用 Pandas 从 3 个数据帧中创建 3 个 HTML 表格。我想要的输出是一个 HTML 文件。我目前使用的代码在另一个下打印表格。我想在上面打印一张桌子,然后并排打印另外两张桌子。我可以在代码中进行哪些更改来实现这一目标?

import numpy as np
from numpy.random import randn
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(randn(5,4),columns='W X Y Z'.split())
df1 = pd.DataFrame(randn(5,4),columns='A B C D'.split())
df2 = pd.DataFrame(randn(5,4),columns='E F G K'.split())
with open("a.html", 'w') as _file:
    _file.write(df.head().to_html() + "\n\n" + df1.head().to_html()+ "\n\n" + df2.head().to_html())

【问题讨论】:

    标签: python html pandas dataframe


    【解决方案1】:

    这是我根据您的原始代码提出的建议:

    import numpy as np
    from numpy.random import randn
    import pandas as pd
    import matplotlib.pyplot as plt
    df = pd.DataFrame(randn(5,4),columns='W X Y Z'.split())
    df1 = pd.DataFrame(randn(5,4),columns='A B C D'.split())
    df2 = pd.DataFrame(randn(5,4),columns='E F G K'.split())
    
    html = """
        {table1}
        <table>
          <tr>
            <td>{table2}</td>
            <td>{table3}</td>
          </tr>
        </table>
        """.format(
            table1=df.head().to_html(),
            table2=df1.head().to_html(),
            table3=df2.head().to_html()
        )
    with open("a.html", 'w') as _file:
        _file.write(html)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2016-02-27
      • 2015-12-26
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多