<p>您必须按照<code>.loc</code>使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.IndexSlice.html" rel="nofollow noreferrer"><strong>^{<cd1>}</strong></a></p>
<p>像这样</p>
<pre><code>idx = pd.IndexSlice[['Total1', 'Total2'], :]
# If you don't want to hard-code use this
idx = pd.IndexSlice[df.index[[2, 5]], :]
</code></pre>
<p>根据需要制作样式功能</p>
<pre><code># 1
def make_bold(val):
return 'font-weight: bold'
# For italic use 'font-style: italic'
# 2
def apply_color(s):
if s.isin(['In']).any():
return ['color: green' for val in s]
return ['color: red' for val in s]
</code></pre>
<p>在元素方面使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.io.formats.style.Styler.applymap.html" rel="nofollow noreferrer"><strong>^{<cd3>}</strong></a>,在列/行方面使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.io.formats.style.Styler.apply.html" rel="nofollow noreferrer"><strong>^{<cd4>}</strong></a></p>
<pre><code>s = df.style.applymap(
make_bold, subset=pd.IndexSlice[["Total1", "Total2"], :] # subset=idx
).apply(apply_color, axis=1)
s
</code></pre>
<p>输出:</p>
<p><a href="https://i.stack.imgur.com/JwYVL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JwYVL.png" alt="enter image description here"/></a></p>
<hr/>
<p>对于#3</p>
<p>您不能在<code>index</code>或<code>columns</code>上应用样式
另请参见<a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Limitations" rel="nofollow noreferrer">^{<cd7>}</a>,在</strong>限制</strong>部分</p>
<blockquote>
<p>You can only style the values, not the index or columns</p>
</blockquote>