Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 python pandas to html with formatting
#1
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:

İmage
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Properly Formatting Pandas Display eagle 2 79 Apr-03-2020, 03:04 PM
Last Post: j.crater
  Pandas df.read_html dropping duplicate tables in html : Urgent Guidance radioactive9 1 1,253 Aug-05-2017, 04:48 AM
Last Post: radioactive9

Forum Jump:


Users browsing this thread: 1 Guest(s)