如何将不同的条件格式应用于不同的行? [英] How to apply different conditional format to different rows?
问题描述
我有以下数据框:[1]: https://i.stack.imgur.com/wBi6V.png
I have the following dataframe: [1]: https://i.stack.imgur.com/wBi6V.png
我使用以下代码突出显示每个单元格的最大值:
I used the following code to highlight the max values of every cell:
def highlight_max(s):
try:
is_max = s == s.max()
return ['background-color: red' if v else '' for v in is_max]
except TypeError:
pass
except KeyError:
pass
s = df.style.apply(highlight_max, axis=1)
s
但是我只想将此格式应用于特定的行,例如每股账面价值(mrq)"行.我找不到任何可以帮助我解决此问题的代码!!!
But I just want to apply this formatting to a certain row, for example the row 'Book Value Per Share (mrq)'. I can't find any code that helps me with this!!!
有人知道吗?
推荐答案
您可以使用 subset
参数,像这样传递一个元组(row_indexer,column_indexer)
.
You can use the subset
argument, passing a tuple (row_indexer, column_indexer)
like so...
s = df.style.apply(highlight_max, axis=1, subset=(0, df.columns))
行索引从零开始,因此 0
代表要格式化的第一行.
The row indices are zero-based, so 0
represents the first row, that you want to format.
More details here in the official docs here: Finer control: slicing
这篇关于如何将不同的条件格式应用于不同的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!