html颜色代码表odd,使用样式和css更改pandas数据框html表python中文本的颜色

这需要几个步骤:

第一次导入HTML和refrom IPython.display import HTML

import re

您可以通过to_html方法获得pandas输出的html。df_html = df.to_html()

接下来,我们将为要创建的html表和样式生成一个随机标识符。random_id = 'id%d' % np.random.choice(np.arange(1000000))

因为我们要插入一些样式,所以需要注意指定此样式仅适用于我们的表。现在我们把这个插入到df_htmldf_html = re.sub(r'

并创建样式标记。这真的取决于你。我刚刚添加了一些悬停效果。style = """

table#{random_id} tr:hover {{background-color: #f5f5f5}}

""".format(random_id=random_id)

最后,展示出来HTML(style + df_html)

功能一体。def HTML_with_style(df, style=None, random_id=None):

from IPython.display import HTML

import numpy as np

import re

df_html = df.to_html()

if random_id is None:

random_id = 'id%d' % np.random.choice(np.arange(1000000))

if style is None:

style = """

table#{random_id} {{color: blue}}

""".format(random_id=random_id)

else:

new_style = []

s = re.sub(r'?style>', '', style).strip()

for line in s.split('\n'):

line = line.strip()

if not re.match(r'^table', line):

line = re.sub(r'^', 'table ', line)

new_style.append(line)

new_style = ['']

style = re.sub(r'table(#\S+)?', 'table#%s' % random_id, '\n'.join(new_style))

df_html = re.sub(r'

return HTML(style + df_html)

像这样使用:HTML_with_style(df.head())

iSiyr.pngHTML_with_style(df.head(), '')

LfFrq.pngstyle = """

tr:nth-child(even) {color: green;}

tr:nth-child(odd) {color: aqua;}

"""

HTML_with_style(df.head(), style)

a95aW.png

学习CSS然后发疯!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值