The data looks like the below
Code Col1 Col2 col3 col4 col5
0 123 5 66 1.7 7
1 123 1 4 12 -8
2 123 9 5 -7 0
3 123 34.4 0 4.789 2
i should be able to ignore applying the style on some column(example in this case col1) doesn't have to be always col1 i should be able to ignore it by specifying the name of the column.
Not every column should have the same style, example for col 2 and 5 i want to color in green positive values and in red negative values however for col3 i want to color in purple values below 50 and yellow the rest
The actual dataset has dozens of columns which different conditions to each regarding the coloring code which is function of column name.
what i have tried:
import pandas as pd
import numpy as np
df = pd.read_excel('Data.xls', sheet_name='test')
styler = df.style
def _color_red_or_green(val):
color = 'red' if val < 0 else 'green'
return 'color: %s' % color
styler.applymap(_color_red_or_green)
styler.to_excel('Output.xlsx')
but this doesn't give me any way to specify column name although it did color all my data red or green, i tried to pass the column name as parameter for _color_red_or_green
for col in df.dtypes.items():
styler.applymap(_color_red_or_green(col[0]))
and adjusted the function accordingly but then had an exception TypeError: the first argument must be callable on styler.to_excel('Output.xlsx') line.