<div id="js_content">
<p style="text-align: center"><a href="http://mp.weixin.qq.com/s?__biz=MzI2NjY5NzI0NA%3D%3D&chksm=ea8b61bcddfce8aaad6bd2a2aee3f66e1bc5ea03dc76b24d8223f84a8fd7b24d0ee7f7622e82&idx=2&mid=2247487183&scene=21&sn=eeddaad7d89459a9ed3a91fde25ee3be#wechat_redirect"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-95993070c19674f7cdaf6b30e43177f5"></a></p>
<p>作者:牵引小哥</p>
<p>来源:牵引小哥讲Python</p>
<p style="text-align: center"><img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8392a8f32eaf7f756b19fc7dc83ad4cc"></p>
<h2>1. 创建样式</h2>
<p>传递样式函数的方法:</p>
<ul><li><p><code>Styler.applymap</code>: 逐个元素,返回带有CSS属性-值对的单个字符串。</p></li><li><p><code>Styler.apply</code>: 列/行/表方式,返回形状相同的Series或DataFrame,其中每个值都是带有CSS属性值对的字符串。</p></li></ul>
<p><code>Styler.applymap</code> 作用于DataFrame中的每一个元素。<code>Styler.apply</code> 通过<code>axis</code>参数,每一次将一列或一行或整个表传递到DataFrame中。对于按列使用 <code>axis=0</code>, 按行使用 <code>axis=1</code>, 整个表使用 <code>axis=None</code>.</p>
<pre class="blockcode"><code class="language-go">import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
axis=1)
df.iloc[3, 3] = np.nan
df.iloc[0, 2] = np.nan
</code></pre>
<pre class="blockcode"><code class="language-go"># 默认无样式
df.style
</code></pre>
<img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-422ae4a965de1d7496e385ef4c229f22">
<p><strong>「注意」</strong>: <code>DataFrame.style</code> 返回Styler对象的属性。</p>
<pre class="blockcode"><code class="language-go"># 通过调用.render方法来查看它们
df.style.highlight_null().render().split('\n')[:10]
</code></pre>
<pre class="blockcode"><code class="language-go">['<style type="text/css" >',
' #T_98ef3b58_b54d_11ea_87c2_8056f2b2fdccrow0_col2 {',
' background-color: red;',
' } #T_98ef3b58_b54d_11ea_87c2_8056f2b2fdccrow3_col3 {',
' background-color: red;',
' }</style><table id="T_98ef3b58_b54d_11ea_87c2_8056f2b2fdcc" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >A</th> <th class="col_heading level0 col1" >B</th> <th class="col_heading level0 col2" >C</th> <th class="col_heading level0 col3" >D</th> <th class="col_heading level0 col4" >E</th> </tr></thead><tbody>',
' <tr>',
' <th id="T_98ef3b58_b54d_11ea_87c2_8056f2b2fdcclevel0_row0" class="row_heading level0 row0" >0</th>',
' <td id="T_98ef3b58_b54d_11ea_87c2_8056f2b2fdccrow0_col0" class="data row0 col0" >1.000000</td>',
' <td id="T_98ef3b58_b54d_11ea_87c2_8056f2b2fdccrow0_col1" class="data row0 col1" >1.329212</td>']
</code></pre>
<h3>编写一个简单的样式函数,该函数会将负数涂成红色,将正数涂成黑色。</h3>
<pre class="blockcode"><code class="language-go">def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'red' if val < 0 else 'black'
return 'color: %s' % color
</code></pre>
<p>在这种情况下,单元格的样式仅取决于其自身的值。我们应该使用在 <code>Styler.applymap</code>作用于每个元素。</p>
<pre class="blockcode"><code class="language-go">df.style.applymap(color_negative_red)
</code></pre>
<img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-b985f785c44ba67f900fa192cbbc750c">
<h3>定义一个突出显示每列中的最大值的函数。</h3>
<pre class="blockcode"><code class="language-go">def highlight_max(s):
'''
highlight the maximum in a Series yellow.
'''
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]
</code></pre>
<pre class="blockcode"><code class="language-go">df.style.apply(highlight_max)
</code></pre>
<img src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8160f9752943cb9a03dd2f365d0707a6">
<h4>实际上,已有定义好的各类高亮函数:</h4>
<p>配合<code>axis</code>参数使用</p>
<ul><li><p>highlight_max():高亮最大值</p></li><li><p>highlight_min():高亮最小值</p></li><li><p>highlight_null():高亮空值</p></li></ul>
<pre class="blockcode"><code class="language-go"># 高亮行最大值
df.style.highlight_max(axis=1)
</c |
|