Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas style background gradient not showing in jupyter notebook

I am trying to print a pandas dataframe with a background gradient for better readability. I tried to apply what I found in the docs to a simple use case, but I can't get jupyter notebook to actually print the table with the colors - I keep getting the plain dataframe. Small example:

import seaborn as sns
import pandas as pd


cm = sns.light_palette('green', as_cmap=True)
df_res = pd.DataFrame(index =['foo','bar'],columns = ['Value 1','Value 2','Value 3'])
df_res.loc['foo'] = [-.5*100, .3,.2]
df_res.loc['bar'] = [.3*100, .6,.9]
df_res.style.background_gradient(cmap=cm)

which just prints

this simple dataframe.

I tried different printing techniques, i.e.

pretty = df_res.style.background_gradient(cmap=cm)
display(pretty)

or

print(pretty)

or a different colormap

df_res.style.background_gradient(cmap='viridis')

but none of them work. I also tried if the styler works at all, but at least the applymap function does what it's supposed to:

def color_negative_red(val):
    """
    Takes a scalar and returns a string with
    the css property `'color: red'` for negative
    strings, black otherwise.
    """
    color = 'red' if val < 0 else 'black'
    return 'color: %s' % color
df_res.style.applymap(color_negative_red)

which prints

So not sure why the background_gradient doesn't seem to have any effect.

EDIT: Just found the reason. It's a simple fix but in case someone else struggles with the same problem I'm keeping this up. Apparently pandas initialized the dataframe with the elements being objects instead of floats. So simple changing initialization to

df_res = pd.DataFrame(index =['foo','bar'],columns = ['Value 1','Value 2','Value 3']).astype('float')

solved the issue.

like image 762
emilaz Avatar asked Dec 09 '19 15:12

emilaz


People also ask

How do I use conditional formatting in pandas?

One way to conditionally format your Pandas DataFrame is to highlight cells which meet certain conditions. To do so, we can write a simple function and pass that function into the Styler object using . apply() or .

Can you use pandas in Jupyter notebook?

In JupyterLab, create a new (Python 3) notebook: In the first cell of the notebook, you can import pandas and check the version with: Now you are ready to use pandas, and you can write your code in the next cells.


1 Answers

Your dtypes of your dataframe are 'object' and not numeric.

First, change the dtype in your dataframe to numeric.

df_res.apply(pd.to_numeric).style.background_gradient(cmap=cm)

Output: enter image description here


Note dtypes:

import seaborn as sns
import pandas as pd


cm = sns.light_palette('green', as_cmap=True)
df_res = pd.DataFrame(index =['foo','bar'],columns = ['Value 1','Value 2','Value 3'])
df_res.loc['foo'] = [-.5*100, .3,.2]
df_res.loc['bar'] = [.3*100, .6,.9]
df_res.info()

Output:

<class 'pandas.core.frame.DataFrame'>
Index: 2 entries, foo to bar
Data columns (total 3 columns):
Value 1    2 non-null object
Value 2    2 non-null object
Value 3    2 non-null object
dtypes: object(3)
memory usage: 144.0+ bytes
like image 94
Scott Boston Avatar answered Sep 24 '22 04:09

Scott Boston