2

Before this is marked as duplicate, I have tried the code from the following topics and none has worked for me thus far:

[Colouring one column of pandas dataframe ]

[Format the color of a cell in a panda dataframe according to multiple conditions ]

[how to color selected columns in python dataframe? ]

I have code that produces three pandas dataframe that looks like this:

         RowName   Orders   Market  StartTime  StopTime
Status
good     A          9       gold    10:00:00    10:09:45
                             .         
                             .
                             .
bad      B          60      silver  07:54:43    08:02:12

         RowName   Orders   Market  StartTime  StopTime
Status
good     E          19      plat.    10:00:00    10:09:45
                             .         
                             .
bad      F          54      mercury  07:54:43    08:02:12

         RowName   Orders   Market  StartTime  StopTime
Status
great     D          3       alum.   10:00:00    10:09:45
                             .         
                             .
ok        C          70      bronze  07:54:43    08:02:12

where the Status column is set as the index of each frame

For each frame, I want to highlight the StartTime column with the value #D42A2A (aka red) regardless of what value is in a given cell.

How can this be done?

MOST RECENT FAILED ATTEMPTS:

  1. def column_style(col): if col.Name == 'StartTime': return pd.Series('bgcolor: #d42a2a', col.index)

  2. def col_color(data): color = 'red' if data != '' else 'black' return 'color: %s' %color frame.style.applymap(col_color, subset=['StartTime'])

but this also fails.

NOTE:

  1. I am using VI within a linux shell

  2. The entire script is being called by IE (internet explorer) so the output of the script is html

  3. I am using BS (beautifulsoup) to scrape data from a few sites and the aggregating the results onto one page {*after scraping the initial website and creating the required website (call it Page1), I tried to scrape Page1 in the same script and add in the html lines via the .attrs method, but this "fails", i.e. the webserver times out during the run}

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
swagless_monk
  • 441
  • 6
  • 21

2 Answers2

4

Let's try this:

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})

df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
               axis=1)
df.iloc[0, 2] = np.nan

def highlight_column(s, col):
    return ['background-color: #d42a2a' if s.name == col else '' for v in s.index]

df.style.apply(highlight_column, col = 'B')

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
0

If anyone is using BeautifulSoup to parse a website and then using pandas to create a DataFrame that you may want to add styles to, you can do something like:

(before using this you have already; imported beautifulsoup, scraped your site and created your dataframe)

variable_name = beautifulsoup(dataframe_name.to_html())

list = []

`for table in variable_name.findAll('table'):`

  `for tbody in variable_name.findAll('table'):`

    `for td in variable_name.findAll('tbody'):`

         `list.append(td)`

list[td_index]['attribute_name'] = 'attribute_value'

This will add your all your table data to a list and you can select any element from that list and add/update a tag attribute

(if more efficient way please comment to help improve)

swagless_monk
  • 441
  • 6
  • 21