Python-df列中特定子字符串的颜色格式

时间:2018-08-15 15:17:46

标签: python pandas formatting

说我有一个df,列为“消息”,如下所示:

df
index    Message
0        John Jones [26/3/18 09:36]: Hey guys, how are you doing?
1        Jack Thomson [26/3/18 09:38]: Hey John, good, how are you?
2        Jane Johnson [26/3/18 09:41]: Did you have a good weekend?
...

我想将所有名称和日期都涂成红色(包括标点符号),同时将后续消息保留为黑色。此列的最终输出将在PyVis软件包支持的交互式图形中。到目前为止,我已经尝试了两种方法,但是都失败了:

方法1:

class Colour:
RED = '\033[91m'
END = '\033[0m'

df['Message'] = (Colour.RED + df['Message'])
df['Message'] = [']:Colour.END'.join(line.split(']:', 1)) for line in df['Message']]

生产(无颜色):

[91mJohn Jones [26/3/18 09:36]:Colour.END Hey guys, how are you doing?

方法2(使用Colorama):

df['Message'] = ('{Fore.RED}' + df['Message'])
df['Message'] = [']:{Style.RESET_ALL}'.join(line.split(']:', 1)) for line in df['Message']]

产生(无颜色):

{Fore.RED} John Jones [26/3/18 09:36]:{Style.RESET_ALL} hey guys, how are you doing?

这使我发疯。是PyVis软件包不支持此功能,还是我的代码有问题?

0 个答案:

没有答案