import numpy as np
import pandas as pd
df = pd.DataFrame([[8484,-1.15],[1390,3.5],[327,10.5],[1135,3.3],[6499,2.0],[1055,4.0]], columns=['data1','dat2'])
def color_rule(val):
return ['background-color: red' if x >= 3 else 'background-color: yellow' for x in val]
html_column = df.style.apply(color_rule, axis=1, subset=['dat2'])
html_column.to_excel('styled.xlsx', engine='openpyxl')
html_column
import numpy as np
import pandas as pd
df = pd.DataFrame([['ex1',8484,-1.15],['ex2',1390,3.5],['ex3',327,10.5],['ex4',1135,3.3],['ex5',6499,2.0],['ex6',1055,4.0]], columns=['tag','data1','dat2'])
def color_rule(tag):
var1 = df['dat2'][df['tag'] == tag.values[0]]
return ['background-color: red' if x >= 3 else 'background-color: yellow' for x in var1]
html_column = df.style.apply(color_rule, axis=1, subset=['tag'])
html_column.to_excel('styled.xlsx', engine='openpyxl')