【问题标题】:Coloring the excel output in Sklearn在 Sklearn 中为 excel 输出着色
【发布时间】:2018-08-16 11:00:50
【问题描述】:

我想根据单个列(我的数据框中的 max_probabilities 列)有条件地格式化我导出的输出数据框(输出格式:excel 文件)的每一行。 如果 max_probabilities 中的概率大于 0.75,我希望将特定的整行涂成绿色,否则它必须涂成红色。 我该怎么做。(注意:我想为导出的 excel 行而不是数据框着色) 数据框格式代码:

df=pd.DataFrame({'Details':x_test,'Amount':test_data.xn_Amount,'Category':Classified_Category,'Probability':max_probabilities})

这就是我导出的数据框现在的样子。

谢谢

【问题讨论】:

    标签: python pandas dataframe colors scikit-learn


    【解决方案1】:

    使用conditional formats,但它只为列着色:

    import string
    
    df = pd.DataFrame({'Amount':[1,2,3],
                       'max_probabilities':[.1,2,.3]})
    print (df)
       Amount  max_probabilities
    0       1                0.1
    1       2                2.0
    2       3                0.3
    

    writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')
    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']
    red_format = workbook.add_format({'bg_color':'red'})
    green_format = workbook.add_format({'bg_color':'green'})
    
    #dict for map excel header, first A is index, so omit it
    d = dict(zip(range(25), list(string.ascii_uppercase)[1:]))
    #print (d)
    
    col = 'max_probabilities'
    excel_header = str(d[df.columns.get_loc(col)])
    #get length of df
    len_df = str(len(df.index) + 1)
    rng = excel_header + '2:' + excel_header + len_df
    print (rng)
    C2:C4
    
    worksheet.conditional_format(rng, {'type': 'cell',
                                          'criteria': '<',
                                           'value':     0.75,
                                           'format': red_format})
    
    worksheet.conditional_format(rng, {'type': 'cell',
                                          'criteria': '>=',
                                           'value':   0.75,
                                           'format':  green_format})
    writer.save()
    

    如果想要着色行:

    df = pd.DataFrame({'Amount':[1,2,3],
                       'Category':['a','d','f'],
                       'max_probabilities':[.1,2,.3]})
    print (df)
       Amount Category  max_probabilities
    0       1        a                0.1
    1       2        d                2.0
    2       3        f                0.3
    
    def highlight(x):
        c1 = 'background-color: green'
        c2 = 'background-color: red' 
        #if want set no default colors 
        #c2 = ''  
        m = x['max_probabilities'] > .75
        df1 = pd.DataFrame(c2, index=x.index, columns=x.columns)
        df1.loc[m, :] = c1
        return df1
    
    df.style.apply(highlight, axis=None).to_excel('styled.xlsx', engine='openpyxl')
    

    【讨论】:

    • 非常感谢@jezrael 我可以知道如何为整行着色吗?
    • @PoorneshV - 检查第二个解决方案,因为第一个行不起作用,因为测试每个单元格。
    猜你喜欢
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多