【发布时间】:2017-03-04 09:06:33
【问题描述】:
官方document 提供了使用to_html(justify='left/right') 设置单元格对齐的选项,并且可以正常工作。但是,尚不清楚如何证明非标题行的合理性。
我必须使用 hack 来替换 HTML 部分
import pandas as pd
df = pd.DataFrame({'looooong_col':['1,234','234,567','3,456,789'],'short_col':[123,4,56]})
raw_html = df.to_html()
raw_html.replace('<tr>','<tr style="text-align: right;">')
所以现在修改后的html是
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>looooong_col</th>
<th>short_col</th>
</tr>
</thead>
<tbody>
<tr style="text-align: right;">
<th>0</th>
<td>1,234</td>
<td>123</td>
</tr>
<tr style="text-align: right;">
<th>1</th>
<td>234,567</td>
<td>4</td>
</tr>
<tr style="text-align: right;">
<th>2</th>
<td>3,456,789</td>
<td>56</td>
</tr>
</tbody>
</table>
它在http://htmledit.squarefree.com/ 中呈现正常,但当我将其写入 html 文件时,单元格仍然是左对齐的。
如何解决这个问题?
【问题讨论】:
-
试试 CSS。在这里查看我的解决方案。 Align text in Pandas column
标签: html pandas html-table justify