Python Forum
python pandas to html with formatting
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:

[Image: lds36.png]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 961 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Properly Formatting Pandas Display eagle 2 1,923 Apr-03-2020, 03:04 PM
Last Post: j.crater
  Pandas df.read_html dropping duplicate tables in html : Urgent Guidance radioactive9 1 2,717 Aug-05-2017, 04:48 AM
Last Post: radioactive9

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020