简体   繁体   English

熊猫风格背景渐变未显示在 jupyter 笔记本中

[英]Pandas style background gradient not showing in jupyter notebook

I am trying to print a pandas dataframe with a background gradient for better readability.我正在尝试打印带有背景渐变的 Pandas 数据框以提高可读性。 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.我试图将我在文档中找到的内容应用于一个简单的用例,但我无法让 jupyter notebook 实际打印带有颜色的表格 - 我一直在获取纯数据框。 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只是打印

这个简单的数据框 . .

I tried different printing techniques, ie我尝试了不同的印刷技术,即

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:我也尝试过样式器是否有效,但至少 applymap 函数可以完成它应该做的事情:

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.所以不确定为什么 background_gradient 似乎没有任何效果。

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.显然,pandas 用元素作为对象而不是浮点数来初始化数据帧。 So simple changing initialization to如此简单的将初始化更改为

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

solved the issue.解决了这个问题。

Your dtypes of your dataframe are 'object' and not numeric.您的数据框的 dtypes 是“对象”而不是数字。

First, change the dtype in your dataframe to numeric.首先,将数据框中的 dtype 更改为数字。

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

Output:输出: 在此处输入图片说明


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM