代码之家  ›  专栏  ›  技术社区  ›  Bartek Malysz

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

  •  2
  • Bartek Malysz  · 技术社区  · 6 年前

    总结 直接从jupyter牢房呼叫

    summary #(Shift + Enter)
    

    我怎样才能使突出显示的行(最后一行)用粗体字?

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  10
  •   John    6 年前

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

    对于您想要的,可以使用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)
    

    输出如下:

    enter image description here

        2
  •  2
  •   Alok Mishra    5 年前

    试试这个:

    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