0

I have a project where I am creating 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: no formatting.

Output should look something like this: enter image description here

2
  • It looks like you need formatters argument from DataFrame.to_html(). Please, refer to this link Commented Mar 26, 2020 at 17:29
  • What have you tried this far? Commented Mar 26, 2020 at 17:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.