比较数据框中的两列并使用pandas样式突出显示值

时间:2018-12-18 13:43:48

标签: python pandas pandas-styles

我正在尝试使用pandas样式突出显示数据框中某些列中的某些值:

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

def highlight_greater(row):

    color=""
    if row['B'] > row['C']:
       color = 'red'
    elif row['D'] > row['E']:
        color = 'gray'

    background = ['background-color: {}'.format(color) for _ in row]
    return background

with open ('out.html','w') as out:
    print >> out, df.style.apply(highlight_greater, axis=1).render()

这很好,但与我的objectif并不对应,我只想突出显示B和D列。如果匹配条件,此脚本将突出显示该行中的所有列。 任何想法 ?谢谢

1 个答案:

答案 0 :(得分:1)

您可以更改样式的DataFrame的自定义功能:

def highlight_greater(x):
    r = 'red'
    g = 'gray'

    m1 = x['B'] > x['C']
    m2 = x['D'] > x['E']

    df1 = pd.DataFrame('background-color: ', index=x.index, columns=x.columns)
    #rewrite values by boolean masks
    df1['B'] = np.where(m1, 'background-color: {}'.format(r), df1['B'])
    df1['D'] = np.where(m2, 'background-color: {}'.format(g), df1['D'])
    return df1


df.style.apply(highlight_greater, axis=None)
相关问题