Python pandas 数据框保存为HTML页面 [英] Python Pandas Data Frame save as HTML page

查看:871
本文介绍了Python pandas 数据框保存为HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将定义在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 =1class =dataframe> < THEAD> < tr style =text-align:right;> <的第i;< /第> <的第i; foo1< /第> <的第i; foo2的< /第> < / TR> < / THEAD> < TBODY> < TR> <的第i; 0℃; /第> < TD> -0.223430< / TD> < TD> -0.904465< / TD> < / TR> < TR> <的第i; 1·/第> < TD> 0.317316< / TD> < TD> 1.321537< / TD> < / TR> < / tbody>< / table>


I am trying to save defined in Python Pandas Data Frame as HTML page. In addition i would like to make this table saved as HTML table ability to be filtered by value of any column. Can you please provide possible solution? At the final this should be table saved as HTML page. I would like to incorporate this code in my Python code. Thank you

解决方案

You can use pandas.DataFrame.to_html().

Example:

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

This will save the following html to filename.html.

Output:

<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页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆