Highlight Row In A Data Frame Based On A Column Value?
Let say I have dataframe like this: col1 col2 col3 col4 0 A A_1 pass 2 1 A A_2 pass 4 2 A A_1
Solution 1:
You can create DataFrame of styles with Styler.apply
and set rows by masks with DataFrame.mask
:
def color(x):
c1 = 'background-color: green'
c2 = 'background-color: red'
c = ''
m1 = x.col3.eq('pass')
m2 = x.col3.eq('fail')
df1 = pd.DataFrame(c, index=x.index, columns=x.columns)
df1 = df1.mask(m1, c1).mask(m2, c2)
return df1
df.style.apply(color,axis=None).to_excel('styled.xlsx', engine='openpyxl', index=False)
Solution 2:
You can also do like that:
from openpyxl import load_workbook
from openpyxl.styles import PatternFill
df.to_excel('my dataframe.xlsx', index=False)
wb = load_workbook('my dataframe.xlsx')
ws = wb.active
forrowin ws.iter_rows(min_col=ws.min_column, max_col=ws.max_column,
min_row=ws.min_row+1, max_row=ws.max_row):
forvalueinrow:
if row[2].value=='pass':
value.fill = PatternFill(fill_type='solid', start_color='00b300', end_color='00b300')
else:
value.fill = PatternFill(fill_type='solid', start_color='ff0000', end_color='ff0000')
wb.save('my dataframe.xlsx')
Result:
Post a Comment for "Highlight Row In A Data Frame Based On A Column Value?"