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



需要用不同的颜色给列上色。使用以下代码,但不使用直线获取邮件中的数据。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    

def highlight_col(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(highlight_col,axis=0(.hide_index((.set_table_styles(styles(.set_option('Check'(

最新更新