Python Pandas 数据框另存为 HTML 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32430009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Pandas Data Frame save as HTML page
提问by Felix
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
我正在尝试将定义的保存Python Pandas Data Frame
为HTML
页面。此外,我想将此表另存为HTML
可以按任何列的值过滤的表能力。你能提供可能的解决方案吗?最后,这应该是表格保存为HTML
页面。我想将此代码合并到我的Python
代码中。谢谢
采纳答案by Sait
You can use pandas.DataFrame.to_html()
.
您可以使用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
.
这会将以下 html 保存到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>
回答by Valery Ramusik
.to_html() also can be used to create html string
.to_html() 也可用于创建 html 字符串
import io
import pandas as pd
from numpy.random import randn
df = pd.DataFrame(
randn(5, 4),
index = 'A B C D E'.split(),
columns = 'W X Y Z'.split()
)
str_io = io.StringIO()
df.to_html(buf=str_io, classes='table table-striped')
html_str = str_io.getvalue()
print(html_str)
<table border="1" class="dataframe table table-striped">
<thead>
<tr style="text-align: right;">
<th></th>
<th>W</th>
<th>X</th>
<th>Y</th>
<th>Z</th>
</tr>
</thead>
<tbody>
<tr>
<th>A</th>
<td>0.302665</td>
<td>1.693723</td>
<td>-1.706086</td>
<td>-1.159119</td>
</tr>
<tr>
<th>B</th>
<td>-0.134841</td>
<td>0.390528</td>
<td>0.166905</td>
<td>0.184502</td>
</tr>
<tr>
<th>C</th>
<td>0.807706</td>
<td>0.072960</td>
<td>0.638787</td>
<td>0.329646</td>
</tr>
<tr>
<th>D</th>
<td>-0.497104</td>
<td>-0.754070</td>
<td>-0.943406</td>
<td>0.484752</td>
</tr>
<tr>
<th>E</th>
<td>-0.116773</td>
<td>1.901755</td>
<td>0.238127</td>
<td>1.996652</td>
</tr>
</tbody>
</table>
回答by sparrow
Here is a way to write the pandas table without using to_html, also including an external stylesheet:
这是一种不使用 to_html 编写 pandas 表的方法,还包括一个外部样式表:
html_string_start = '''
<html>
<head><title>Report Title</title></head>
<link rel="stylesheet" type="text/css" href="mystyle.css"/>
<body>
'''
html_string_end = '''
</body>
</html>
'''
with open(r'c:\temp\myfile.html', 'w') as f:
f.write(html_string_start)
f.write('<table>')
for header in dataframe.columns.values:
f.write('<th>'+str(header)+'</th>')
for i in range(len(dataframe)):
f.write('<tr>')
for col in dataframe.columns:
value = dataframe.iloc[i][col]
f.write('<td>'+str(value)+'</td>')
f.write('</tr>')
f.write('</table>')
f.write(html_string_end)