如何在转换为html时向特定数据框列添加背景色

2024-04-25 09:27:10 发布

您现在位置:Python中文网/ 问答频道 /正文

需要用不同的颜色给柱上色。使用下面的代码,但也不使用直线获取邮件中的数据。To html方法在生成表时工作正常,但需要对Dataframe中的所有列使用不同的颜色

pdf.style.apply(highlight_cols, axis=None)

还有一个是

output=df.to_html() 
batch_name=['a','a','p','o'] 
duration=['10','45','65','20'] 
batch_dict={'Batch Name': batch_name,'Duration':duration} 
pdf = pd.DataFrame(batch_dict) 
def highlight_cols(x): 
    pdf = x.copy()   
    pdf[['Batch Name']] = 'background-color: grey'     
    print('pdf x', pdf)    
    return pdf    

Tags: 数据代码namepdf颜色htmlbatch邮件
1条回答
网友
1楼 · 发布于 2024-04-25 09:27:10

def突出显示颜色(x): if x.name in 'B'return ['background-color: #feffc1']*x.shape[0]elif x.name in 'Duration':ereturn ['background-color: #ff9090']*x.shape[0]elsereturn ['background-color: None']*x.shape[0]

pdf=pdf.style.apply(高亮显示列,轴=0)。隐藏索引()。设置表格样式。设置标题('Check'))

相关问题 更多 >