pandas to html 多个0,python - pandas to html with formatting - Stack Overflow

本文介绍如何使用Python的Pandas库将DataFrame转换为带格式的HTML表格,并根据特定条件对单元格进行颜色标记。具体包括针对不同类型数据列(如文本、布尔值、浮点数)设置不同的颜色高亮规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a project where I am creating multiple html tables from pandas dataframes.

Currently I have a pandas dataframe which contains a combination of floats, strings and bools:

import pandas as pd

df = pd.DataFrame()

df['text1'] = ['G90T0','G90T180','G90T0RL CD1','G90T180LL CD1','G90T0RL CD2','G90T180 CD2']

df['text2'] = ['value1','value1','value2','value3','value1','value1']

df['floats1'] = [90.0, 90.0, 90.0, 90.0, 90.0, 90.0]

df['bools1'] = [True, True, True, True, False, True]

df['bools2'] = [True, False, True, True, True, True]

df['floats1'] = [90.0, 90.0, 90.0, 90.0, 90.0, 90.0]

df['floats2'] = [5.2, 4.9, 4, 6, 4.1, 3]

html = df.to_html()

path = "C:\\path"

text_file = open('test.html', "w")

text_file.write(html)

text_file.close()

I am trying to write a formatting function which highlights colors cells red or green depending multiple different conditions. Here are the rules for coloring this specific dataframe:

Columns 'text2': red if not equal to 'value1'

Columns 'boolS1' and 'boolS2': green for True and red for False

Column 'floats2': red if greater than 5, green if equal to or less than 5

Other columns: no formatting.

Output should look something like this:

lds36.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值