Html 条件双极表内条形图-熊猫-css

Html 条件双极表内条形图-熊猫-css,html,css,excel,pandas,bar-chart,Html,Css,Excel,Pandas,Bar Chart,对于我的熊猫数据框(python)的html表示,我想创建尽可能类似于下图(用Excel创建)的东西,也就是说,给定一个数字序列,在表中创建,如果值大于零,一些水平条形图将变为绿色,如果它们低于零且轴上的“零”点根据提供的数据动态重新缩放,则为红色。我最接近的方法是使用熊猫本地仪器,代码如下() #在Jupyter完成 从导入数据帧 df=pd.DataFrame([-0.02,0.03,0.04,-0.05],columns=['A'])) 大于0=df.loc[df.loc[:,'A']>

对于我的熊猫数据框(python)的html表示,我想创建尽可能类似于下图(用Excel创建)的东西,也就是说,给定一个数字序列,在表中创建,如果值大于零,一些水平条形图将变为绿色,如果它们低于零且轴上的“零”点根据提供的数据动态重新缩放,则为红色。我最接近的方法是使用熊猫本地仪器,代码如下()

#在Jupyter完成
从导入数据帧
df=pd.DataFrame([-0.02,0.03,0.04,-0.05],columns=['A']))
大于0=df.loc[df.loc[:,'A']>=0].index.values.tolist()
小于0=df.loc[df.loc[:,'A']<0]。index.values.tolist()
df.style.bar(subset=pd.indexlice[more_than_zero,'A'],color='d65f5f')
#df.style.bar(subset=pd.indexlice[less_-than_-zero,'B'],color='7fff00')

更新:此回答导致一个pull请求在Pandas v0.20.0中被接受。文件可在熊猫网站上找到,网址为


现在使用pandas无法做到这一点(我将尽快尝试实现),但现在有一个猴子补丁解决方案:

def _bar_center_zero(self, s, color_positive, color_negative, width):

    # Either the min or the max should reach the edge (50%, centered on zero)
    m = max(abs(s.min()),abs(s.max()))

    normed = s * 50 * width / (100 * m)

    base = 'width: 10em; height: 80%;'

    attrs_neg = (base+ 'background: linear-gradient(90deg, transparent 0%, transparent {w}%, {c} {w}%, '
                 '{c} 50%, transparent 50%)')

    attrs_pos = (base+ 'background: linear-gradient(90deg, transparent 0%, transparent 50%, {c} 50%, {c} {w}%, '
                 'transparent {w}%)')

    return [attrs_pos.format(c=color_positive,  w=(50+x)) if x > 0
                else attrs_neg.format(c=color_negative, w=(50+x)) 
                    for x in normed]

def bar_excel(self, subset=None, axis=0, color_positive='#5FBA7D', 
                  color_negative='#d65f5f', width=100):
    """
    Color the background ``color`` proptional to the values in each column.
    Excludes non-numeric data by default.
    .. versionadded:: 0.17.1
    Parameters
    ----------
    subset: IndexSlice, default None
        a valid slice for ``data`` to limit the style application to
    axis: int
    color_positive: str
    color_negative: str
    width: float
        A number between 0 or 100. The largest value will cover ``width``
        percent of the cell's width
    Returns
    -------
    self : Styler
    """
    #subset = _maybe_numeric_slice(self.data, subset)
    #subset = _non_reducing_slice(subset)

    self.apply(self._bar_center_zero, axis=axis, subset=subset, 
               color_positive=color_positive, color_negative=color_negative,
               width=width)
    return self
Monkey将此修补程序添加到Styler类:

pd.formats.style.Styler._bar_center_zero = _bar_center_zero
pd.formats.style.Styler.bar_excel = bar_excel
现在您可以使用它:

df = pd.DataFrame([-0.02, 0.03, 0.04, -0.05], columns=['A'])

df.style.bar_excel(color_positive='#5FBA7D', color_negative='#d65f5f')


更新: 此后,我在GitHub上创建了一个应用程序,并在其中实现了“动态”居中选项(
align='mid'

通过指定选项
align
并根据数据的类型,可以获得的结果栏如下所示:


请注意,我看到了您的要求,即只有在实际情况发生后才需要“动态调整”零位。通过确保将50%放置在
s.max()
s.min()
之间的点上,可以对上述内容进行调整,以确保情况属实。有关在更新版本的pandas上使用此功能的任何更新?>=1.1我的PR被接受了。文档可用,请参见“谢谢Julien!”!这正是我要找的!
df = pd.DataFrame([-0.02, 0.03, 0.04, -0.05], columns=['A'])

df.style.bar_excel(color_positive='#5FBA7D', color_negative='#d65f5f')