在jupyter中更改熊猫数据框的文本部分的颜色

时间:2019-04-24 22:39:13

标签: python pandas jupyter

我正在尝试获取一个数据框,其中一列是一组用分号分隔的条目,并突出显示特定的条目。下面的代码

import pandas as pd
import colored
test = pd.DataFrame(['foo;bar;baz', 'foo', 'bar', 'baz'])
mk = lambda s: colored.bg('red') + s + colored.bg(255) if s in {'bar', } else s 
mark = lambda s: ';'.join(map(mk, s.split(';')))                    
for i, t in test.iterrows():
    print(';'.join(map(mark, t[0].split(';'))))
print(test.applymap(mark))
display(test.style.applymap(mark))

将生成以下渲染图 test image

问题是,如果我对我正在使用的实际数据集使用打印语句,则文本将被截断。是否可以使用display函数中的多种颜色?我会使用数据帧的style属性,但据我所知,这只会突出显示整个单元格,而不是其中一部分。

1 个答案:

答案 0 :(得分:0)

您可以在这里找到答案: enter link description here

我尝试过显示,并且可以正常工作。