Fel*_*lix 21 html python pandas
我试图将Python Pandas Data Frame中定义的保存为HTML页面.另外我想使这个表保存为HTML表能力,可以按任何列的值进行过滤.你能提供可能的解决方案吗?在最后,这应该是表保存为HTML页面.我想将此代码合并到我的Python代码中.谢谢
Sai*_*ait 41
你可以用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')
Run Code Online (Sandbox Code Playgroud)
这将保存以下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>
Run Code Online (Sandbox Code Playgroud)
.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)
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34272 次 |
最近记录: |