Dataframe styling in Jupyter Notebook

Viewed 222

I have a pandas dataframe to which I applied cell coloring based on the values in a second dataframe. (The 2 dataframes are the same size). I did this based on this SO answer shown here: enter image description here

Now that I've colored the dataframe, the cell outlines have disappeared. I saw a suggestion to use the following to add cell outlines:

df_navigator = df_navigator.data.style.set_properties(**{'text-align': 'left','border-color':'Black','border-width':'thin','border-style':'dotted'})

If I do that, then the cell coloring disappears.

How can I keep the custom cell coloring while adding the black borders back in to the displayed dataframe?

Adding the full code for reproducing:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': 'foo bar foo'.split(),
    'B': 'one one two'.split(),
    'C': np.arange(3),
    'D': np.arange(3) * 2
})
j = [
    { 'bgcolor': '#55aa2a'},
    { 'bgcolor': '#d42a2a'},
    { 'bgcolor': '#d42a2a'},
]

df2 = pd.DataFrame({
    'E': j,
    'F': j,
    'G': j,
    'H': j 
})
df2 = df2.applymap(lambda x: 'background-color: {}'.format(x.get('bgcolor')))

def highlight(x):
    return pd.DataFrame(df2.values, columns = x.columns)

df.style.apply(highlight, axis=None)

Thanks in advance!

0 Answers
Related