【问题标题】:Python conditional formatting for coloring of string values用于字符串值着色的 Python 条件格式
【发布时间】:2021-02-08 08:20:13
【问题描述】:

我有一个带有对象列的数据框,如下所示。列包含字符串值,括号中带有分数和增量分数

Manager     engage_final   engage_q1_final    engage_q2_final    engage_q3_final   
John        64% (+5)       66% (+4)           65% (+6)           64% (+5)
Peter       92% (-2)       90% (-1)           91% (-7)           93% (-4) 
Jennifer    98% (nan)      96% (nan)          97% (nan)          96% (nan)
Patricia    95% (0)        95% (-1)           96% (+1)           94% (0)  
Melissa     88% (+3)       85% (+2)           84% (0)            90% (-6)

在将其输出到 Excel 之前,我想根据 delta 分数应用一些条件格式。条件格式涉及根据括号中的值应用一些颜色。 +5 以上为深绿色,+5 为深绿色,+4 为浅绿色,-5 以下为深橙色,-5 为深橙色,-4 为浅橙色,以此类推。

我尝试了以下代码,它不是最优雅的,而且它也不起作用。似乎找不到很多条件格式的解决方案。非常感谢任何形式的帮助,谢谢。

columns = [col for col in engage_df.columns if 'engage' in col]


def highlight (engage_df):
    if engage_df[columns].str.contains("+5"):
        return ['background-color: #64A064']
    elif engage_df[columns].str.contains("+4"):
        return ['background-color: #78B478']
    elif engage_df[columns].str.contains("+3"):
        return ['background-color: #8CC88C']
    elif engage_df[columns].str.contain("+2"):
        return ['background-color: #A0DCA0']
    elif engage_df[columns].str.contains("+1"):
        return ['background-color: #B4F0B4']
    elif engage_df[columns].str.contains("+"):
        return ['background-color: #508C50']
    elif engage_df[columns].str.contains("-5"):
        return ['background-color: #F48200']
    elif engage_df[columns].str.contains("-4"):
        return ['background-color: #FF9600']
    elif engage_df[columns].str.contains("-3"):
        return ['background-color: #FFAA00']
    elif engage_df[columns].str.contains("-2"):
        return ['background-color: #FFBE00']
    elif engage_df[columns].str.contains("-1"):
        return ['background-color: #FFD200']
    elif engage_df[columns].str.contains("-"):
        return ['background-color: #FF6E00']
    else:
        return ['background-color: white']

engage_df.style.apply(highlight, axis=1)

【问题讨论】:

  • 您的意思是当您从 ide 的变量检查器检查数据帧时格式化,还是导出到 excel 时格式化?
  • @Pythonistaan​​onymous,我的意思是当它导出到 excel 时。

标签: python pandas dataframe conditional-formatting xlsxwriter


【解决方案1】:

仅为子集参数中的列创建映射值并传递给Styler.apply 的字典:

def highlight(x):
    #get +-INT values between ()
    s1 = x.str.extract('\(([+-]*\d+)\)', expand=False)
    #Series with default values
    s = pd.Series('background-color: white', index=x.index)
    #dictionary for match
    d = {'+5':'background-color: #64A064', '+4': 'background-color: #78B478'}
    #if match colot between () add value from dictionary
    for k, v in d.items():
        s.loc[s1 == k] = v
         
    #replace >5 and <-5 values comparing floats values
    s.loc[s1.astype(float) > 5] = 'background-color: #508C50'  
    s.loc[s1.astype(float) < -5] = 'background-color: #FF6E00'  
    return s


cols = [col for col in engage_df.columns if 'engage' in col]
engage_df.style.apply(highlight, subset=cols, axis=1).to_excel('file.xlsx')

【讨论】:

  • 这似乎有效!但是有没有办法让任何超过 +5 的值作为 'background-color:: #508C50' 和任何小于 -5 的值作为 'background-color: #FF6E00'
  • @wjie08 - 我想念它。为它添加了解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 2013-07-07
  • 1970-01-01
  • 2018-07-08
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多