Skip to main content
 首页 » 编程设计

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

2024年12月31日8落叶无声

我正在尝试使用 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() 

这很好,但不符合我的目标,我只想突出显示 B 和 D 列。如果匹配条件,此脚本突出显示行中的所有列。 任何想法 ?谢谢

请您参考如下方法:

您可以更改样式的 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)