Python Pandas 数据框另存为 HTML 页面

标签 python html pandas

我正在尝试将 Python Pandas Data Frame 中定义的内容保存为 HTML 页面。此外,我想让这个表保存为 HTML 表能够按任何列的值进行过滤。你能提供可能的解决方案吗?最后这应该是保存为 HTML 页面的表格。我想将此代码合并到我的 Python 代码中。谢谢

最佳答案

您可以使用 pandas.DataFrame.to_html() .

示例:

>>> import numpy as np
>>> from pandas import *
>>> df = DataFrame({'foo1' : np.random.randn(2),
                    'foo2' : np.random.randn(2)})
>>> df.to_html('filename.html')

这会将以下 html 保存到 filename.html

输出:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>foo1</th>
      <th>foo2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-0.223430</td>
      <td>-0.904465</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.317316</td>
      <td>1.321537</td>
    </tr>
  </tbody>
</table>

关于Python Pandas 数据框另存为 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32430009/

相关文章:

python - 使用lxml在python中获取tr的tbody内的所有td内容

python - python中的数据库连接

python - 如何解决 GitLab CI/CD 作业中的 "remote: You are not allowed to upload code."错误?

python - Pandas Rolling vs Scipy kurtosis - 严重的数值不准确

python - Pandas系列——记录数值变化

Python Pandas 以 DF2 列值为条件进行更新

javascript - 在单元格很长的表格中保持表格行内容清晰可见

html - 仅单击图像右上角的元素位置

javascript:无法找出错误!

python - 使用包含列表 Python 的字典过滤 DataFrame 的 2 列