为一列 pandas 数据框着色 [英] Colouring one column of pandas dataframe

查看:49
本文介绍了为一列 pandas 数据框着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,并希望使用.style突出显示我的数据框的第一列谢谢

I have a dataframe and would like to use the .style to highlight the first column of my dataframe thank you

我不确定是否必须使用循环或函数

I wasn't sure if there is a loop I have to use or a function

推荐答案

我认为您需要自定义函数,该函数将第一列的颜色返回DataFrame:

I think you need custom function which return DataFrame with color for first column:

np.random.seed(100)
df =  pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))

def highlight_col(x):
    r = 'background-color: red'
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    df1.iloc[:, 0] = r
    return df1    
df.style.apply(highlight_col, axis=None)

这篇关于为一列 pandas 数据框着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆