2

I have a data frame, df, as below:

Name Day
Bob Monday
John Thursday
Sam Friday
Bob Monday
Katie Sunday
Kyle Tuesday
Katie Saturday
Bob Wednesday
Katie Sunday
Sam Thursday
Joe Friday

The following code highlights entire rows given how often a name occurs within the Name Column:

cmap = {1: 'green', 2: 'yellow', 3: 'red'}
freq = df['Name'].map(df['Name'].value_counts())
colors = freq.map(cmap).radd('background-color: ')

df.style.apply(lambda s: colors)

However when I use .render() in order to convert the styler object to html to send in an email, there is no boarder in the table. How do you keep a boarder when converting a dataframe to a styler object then to an HTML table?

1 Answer 1

6

I had the question virtually typed up when I found the answer so I thought I'd do it as a Q&A.

Stringing styles on one after the other when creating the styler object worked well. For example:

df.style.apply(lambda s: colors).set_table_styles(
    [{"selector": "", "props": [("border", "1px solid")]},
      {"selector": "tbody td", "props": [("border", "1px solid")]},
     {"selector": "th", "props": [("border", "1px solid")]}])

Then within the set_table_styles different parameters can be changed to format the boarder.

Sign up to request clarification or add additional context in comments.

Comments

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.