pandas 仅在一行中以粗体显示 DataFrame() 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51938245/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:57:50  来源:igfitidea点击:

display DataFrame() values in bold font in one row only

pandasipythonjupyter-notebook

提问by Bartek Malysz

having dataframe summarybeing called directly from jupyter cell

直接从 jupyter 单元调用数据帧摘要

summary #(Shift + Enter)

how can I make the highlighted row only (last row) in bold font?

如何仅以粗体显示突出显示的行(最后一行)?

enter image description here

在此处输入图片说明

回答by John

Please, please provide the code to at least generate the dataframe you are posting.

请提供代码以至少生成您发布的数据框。

For what you want, you can use style.applymap() function. The rest is to find the correct properties of font that can be set to bold and how to set index of last row.

对于你想要的,你可以使用 style.applymap() 函数。剩下的就是找到可以设置为粗体的字体的正确属性以及如何设置最后一行的索引。

   In [1]:     
          import pandas as pd
   In [2]:
          def df_style(val):
              return 'font-weight: bold'
   In [3]:            
       summary = pd.DataFrame([[0, 0, 0, 0, 0, ],[1620, 203, 392, 651, 2236]],index=['None','Total'])

       summary.style.applymap('font-weight: bold',
                  subset=pd.IndexSlice[summary.index[summary.index=='Total'], :])
       print(summary)

And below is the output:

下面是输出:

enter image description here

在此处输入图片说明

回答by Alok Mishra

try this:

尝试这个:

def highlight_max(x):
    return ['font-weight: bold' if v == x.loc[4] else ''
                for v in x]
df = pd.DataFrame(np.random.randn(5, 2))
df.style.apply(highlight_max)

enter image description here

在此处输入图片说明