pandas:带条件格式的 HTML 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14627380/
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
pandas: HTML output with conditional formatting
提问by btel
I am trying to format a table, such that data in each column are formatted in a style depending on their values (similar to conditional formatting in spreadsheet programs). How can I achieve that in pandas using the HTML formatter?
我正在尝试格式化表格,以便每列中的数据根据它们的值以某种样式格式化(类似于电子表格程序中的条件格式)。如何使用 HTML 格式化程序在 Pandas 中实现这一点?
A typical use case is highlighting significant values in a table. For example:
一个典型的用例是突出显示表格中的重要值。例如:
correlation p-value
0 0.5 0.1
1 0.1 0.8
2 0.9 *0.01*
pandas allows to define custom formatters for HTML output - to obtain above output one could use:
pandas 允许为 HTML 输出定义自定义格式化程序 - 要获得上述输出,可以使用:
import pandas as pd
from pandas.core import format
from StringIO import StringIO
buf = StringIO()
df = pd.DataFrame({'correlation':[0.5, 0.1,0.9], 'p_value':[0.1,0.8,0.01]})
fmt = format.DataFrameFormatter(df,
formatters={'p_value':lambda x: "*%f*" % x if x<0.05 else str(x)})
format.HTMLFormatter(fmt).write_result(buf)
However, I would like to change the style for significant values (for example, by using bold font).
但是,我想更改重要值的样式(例如,使用粗体)。
A possible solution would be to attach a CSS class to <td>tags in the HTML output, which could be then formatted using CSS stylesheet. The above would then become:
一种可能的解决方案是将 CSS 类附加到<td>HTML 输出中的标签,然后可以使用 CSS 样式表对其进行格式化。上面的内容将变为:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>correlation</th>
<th>p_value</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td> 0.5</td>
<td> 0.10</td>
</tr>
<tr>
<td>1</td>
<td> 0.1</td>
<td> 0.80</td>
</tr>
<tr>
<td>2</td>
<td> 0.9</td>
<td class='significant'> 0.01</td>
</tr>
</tbody>
</table>
Edit: As suggested by @Andy-Hayden I can add formatting by simply replacing stars with <span class="signifcant">...</span>in my example:
编辑:正如@Andy-Hayden 所建议的,我可以通过<span class="signifcant">...</span>在我的示例中简单地替换星星来添加格式:
import pandas as pd
from StringIO import StringIO
buf = StringIO()
significant = lambda x: '<span class="significant">%f</span>' % x if x<0.05 else str(x)
df = pd.DataFrame({'correlation':[0.5, 0.1,0.9], 'p_value':[0.1,0.8,0.01]})
df.to_html(buf, formatters={'p_value': significant})
Newer versions of pandas escape the tags. To avoid it replace last line with:
较新版本的Pandas逃脱了标签。为避免将最后一行替换为:
df.to_html(buf, formatters={'p_value': significant}, escape=False)
采纳答案by Andy Hayden
You can use the DataFrame to_htmlmethod, which comes with formattersargument.
您可以使用to_html带有formatters参数的 DataFrame方法。
An easier solution would be to surround by <span class="significant">and </span>, (rather than *). Note: by default this will be escaped (i.e. <becomes <) so you will need to use the escape=Falseargument.
一个更简单的解决方案是用<span class="significant">和</span>, (而不是*)包围。注意:默认情况下,这将被转义(即<变成<),因此您需要使用该escape=False参数。
回答by joelostblom
Since pandas 0.17.1, it is easy to apply custom formatting to the data frame HTML representation using the styling api.
从 pandas 0.17.1 开始,使用样式 api可以轻松地将自定义格式应用于数据框 HTML 表示 。
import pandas as pd
df = pd.DataFrame({
'correlation':[0.5, 0.1,0.9],
'p_value':[0.1,0.8,0.01]})
styled_df = df.style.apply(
lambda x: ['font-weight: bold; background-color: yellow'
if value <= 0.01 else '' for value in x])
styled_df
The output is rendered automatically in interfaces such as the Jupyter Notebook
and the string representation of the HTML can be returned with the render()method.
输出在 Jupyter Notebook 等接口中自动呈现,并且可以使用该render()方法返回 HTML 的字符串表示形式。
print(styled_df.render())
<style type="text/css" >
#T_4e49b9da_8451_11e8_9166_605718a99a7frow2_col1 {
font-weight: bold;
background-color: yellow;
font-weight: bold;
background-color: yellow;
}</style>
<table id="T_4e49b9da_8451_11e8_9166_605718a99a7f" >
<thead> <tr>
<th class="blank level0" ></th>
<th class="col_heading level0 col0" >correlation</th>
<th class="col_heading level0 col1" >p_value</th>
</tr></thead>
<tbody> <tr>
<th id="T_4e49b9da_8451_11e8_9166_605718a99a7flevel0_row0" class="row_heading level0 row0" >0</th>
<td id="T_4e49b9da_8451_11e8_9166_605718a99a7frow0_col0" class="data row0 col0" >0.5</td>
<td id="T_4e49b9da_8451_11e8_9166_605718a99a7frow0_col1" class="data row0 col1" >0.1</td>
</tr> <tr>
<th id="T_4e49b9da_8451_11e8_9166_605718a99a7flevel0_row1" class="row_heading level0 row1" >1</th>
<td id="T_4e49b9da_8451_11e8_9166_605718a99a7frow1_col0" class="data row1 col0" >0.1</td>
<td id="T_4e49b9da_8451_11e8_9166_605718a99a7frow1_col1" class="data row1 col1" >0.8</td>
</tr> <tr>
<th id="T_4e49b9da_8451_11e8_9166_605718a99a7flevel0_row2" class="row_heading level0 row2" >2</th>
<td id="T_4e49b9da_8451_11e8_9166_605718a99a7frow2_col0" class="data row2 col0" >0.9</td>
<td id="T_4e49b9da_8451_11e8_9166_605718a99a7frow2_col1" class="data row2 col1" >0.01</td>
</tr></tbody>
</table>


