熊猫:带有条件格式的HTML输出

时间:2022-10-29 20:25:10

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格式化程序在熊猫中实现这一点?

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:

熊猫允许定义自定义格式器的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:

一个可能的解决方案是在HTML输出中附加一个CSS类到标记,然后使用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 >我的例子:

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:

更新版本的熊猫会避开标签。为避免将最后一行替换为:

df.to_html(buf, formatters={'p_value': significant}, escape=False)

1 个解决方案

#1


19  

You can use the DataFrame to_html method, which comes with formatters argument.

您可以使用DataFrame to_html方法,它带有formatters参数。

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 &lt;) so you will need to use the escape=False argument.

更简单的解决方案是使用(而不是*)包围。注意:在默认情况下,这将被转义(例如,< become <),因此您将需要使用escape=False参数。

#1


19  

You can use the DataFrame to_html method, which comes with formatters argument.

您可以使用DataFrame to_html方法,它带有formatters参数。

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 &lt;) so you will need to use the escape=False argument.

更简单的解决方案是使用(而不是*)包围。注意:在默认情况下,这将被转义(例如,< become <),因此您将需要使用escape=False参数。