将熊猫格式转换为html

时间:2020-03-17 20:34:31

标签: python html pandas

我有一个项目,该项目是从pandas数据帧创建多个html表。

目前,我有一个熊猫数据框,其中包含浮点数,字符串和布尔值的组合:

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()

我正在尝试编写一种格式化函数,该函数可以根据多种不同情况突出显示红色或绿色的颜色单元。这是为该特定数据框着色的规则:

“ text2”列:如果不等于“ value1”,则为红色

'boolS1'和'boolS2'列:绿色表示True,红色表示False

“ floats2”列:大于5时为红色,小于等于5时为绿色

其他列:无格式。

输出应如下所示: enter image description here

0 个答案:

没有答案