简体   繁体   English

pandas 有条件地格式化粗体字段

[英]pandas conditionally format field in bold

I have a pandas dataframe:我有一个 pandas dataframe:

import pandas as pd
import numpy as np

df = pd.DataFrame({'foo':[1,2, 3, 4],
                   'bar':[[1,2,0.04], [1,2,0.04], [1,2,0.06], np.nan]})
display(df)

def stars(x, sign_level):
    if x is np.nan:
        return ''
    else:
        p_value = x[2]
        if p_value < sign_level:
            return '*'
        else:
            return ''

df['marker'] = df.bar.apply(stars, sign_level=0.05)
df

Instead of adding a column with a star in case the result is considered significant, is it possible to format the cell (like in an Excel sheet) as bold?如果结果被认为是重要的,而不是添加带有星号的列,是否可以将单元格(如在 Excel 表中)格式化为粗体?

display DataFrame() values in bold font in one row only seems to be able to format a whole row - I would like to reformat only a specific cell 在一行中以粗体显示 DataFrame() 值似乎只能格式化一整行 - 我只想重新格式化一个特定的单元格

Conditionally format Python pandas cell 有条件地格式化 Python pandas 单元格

seems similar, though they only change the background, not format as bold.看起来很相似,尽管它们只改变了背景,而不是粗体的格式。

edit编辑

the code below can already change the background color - I just do not know how to format as bold.下面的代码已经可以改变背景颜色 - 我只是不知道如何格式化为粗体。

def highlight_significant(x, sign_level):
    if x is np.nan:
        return ''
    else:
        if isinstance(x, list):
            p_value = x[2]
            color = 'lightgreen' if p_value < sign_level else ''
            if p_value < sign_level:
                return 'background-color: %s' % color
            else:
                return ''
        else:
            return ''

df.style.applymap(highlight_significant, sign_level=0.05)

This might help...这可能会有所帮助...

Set up a dataframe设置一个 dataframe

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[0, 2] = np.nan

create functional you can apply to add bold based on a condition of you define创建功能,您可以根据您定义的条件应用添加粗体

def negative_bold(val):


    bold = 'bold' if val < 0 else ''


    return 'font-weight: %s' % bold

Apply the function to the style of the data frame将 function 应用于数据框的样式

s = df.style.applymap(negative_bold)

Look at the dataframe, you should find all negative numbers are bold查看 dataframe,您应该会发现所有负数都是粗体

在此处输入图像描述

I looked here https://mode.com/example-gallery/python_dataframe_styling/ and here https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html我看了这里https://mode.com/example-gallery/python_dataframe_styling/和这里https://pandas.pydata.org/pandas-docs/stable/user_guide/style.ZFC369FDC70D5FCC786

EDIT Adding to this answer...编辑添加到这个答案......

Combining two styles结合两个styles

I have two functions, one to highlight yellow the number is negative and another make the number bold if negative我有两个功能,一个突出黄色数字是负数,另一个使数字加粗如果负数

Negative_yellow Negative_yellow

def negative_yellow(val):


    color = 'yellow' if val < 0 else ''


    return 'background-color:' + color  

Negative bold负粗体

def negative_bold(val):


    bold = 'bold' if val < 0 else ''


    return 'font-weight: %s' % bold

I apply the two the data frame like this我像这样应用这两个数据框

df.style.\
    applymap(negative_yellow).\
    applymap(negative_bold)

在此处输入图像描述

I imagine there are more elegant ways of doing this.我想有更优雅的方法可以做到这一点。 Hope this helps:)希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM