给定熊猫数据框突出显示一列

时间:2019-08-23 23:06:08

标签: python pandas

我有一个熊猫数据框。我想突出显示其中一列为蓝色。我尝试这样做:

df['column'] = df.style.apply(lambda x: ['background: lightblue' if x.name == 'column' else '' for i in x])

但这不起作用。

2 个答案:

答案 0 :(得分:3)

df.style.apply

method

因此,您不想分配与其相等的列。 style.apply已就位,因此删除分配并仅使用

df.style.apply(lambda x: ['background: lightblue' if x.name == 'column'
                          else '' for i in x])

,它将在适当的位置样式化列。

答案 1 :(得分:1)

此解决方案也有效。

def highlight(s):
    same = s == df['column']
    return ['background-color: lightblue' if x else '' for x in same] 

df.style.apply(highlight)