python pandas to html with formatting - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: python pandas to html with formatting (/thread-25245.html) |
python pandas to html with formatting - slangswinger - Mar-24-2020 I have a project where I am creating a multiple html tables from pandas dataframes. Currently I have a pandas dataframe which contains a combination of floats, strings and bools: import pandas as pd df = pd.DataFrame() df['text1'] = ['G90T0','G90T180','G90T0RL CD1','G90T180LL CD1','G90T0RL CD2','G90T180 CD2'] df['text2'] = ['value1','value1','value2','value3','value1','value1'] df['floats1'] = [90.0, 90.0, 90.0, 90.0, 90.0, 90.0] df['bools1'] = [True, True, True, True, False, True] df['bools2'] = [True, False, True, True, True, True] df['floats1'] = [90.0, 90.0, 90.0, 90.0, 90.0, 90.0] df['floats2'] = [5.2, 4.9, 4, 6, 4.1, 3] html = df.to_html() path = "C:\\path" text_file = open('test.html', "w") text_file.write(html) text_file.close()I am trying to write a formatting function which highlights colors cells red or green depending multiple different conditions. Here are the rules for coloring this specific dataframe: Columns 'text2': red if not equal to 'value1' Columns 'boolS1' and 'boolS2': green for True and red for False Column 'floats2': red if greater than 5, green if equal to or less than 5 Other columns, do not highlight Output should look something like this: |