【问题标题】:How to remove header from pandas Styler?如何从熊猫样式器中删除标题?
【发布时间】:2020-11-09 21:35:27
【问题描述】:

我想从 pandas Styler 中删除标题,以便我可以渲染它。 我尝试过的:

def highlight(x):
    c1 = 'background-color: #f5f5dc'
    df1 = pd.DataFrame('', index=x.index, columns=x.columns)
    df1.loc[['A'], :] = c1
    return df1
temp = {'col1': ['abc', 'def'], 'col2': [1.0, 2.0]}
df = pd.DataFrame(temp)
df.index = ['A', 'B']
print(df)
df.style.apply(highlight, axis=None).hide_index()

输出

col1  col2
-------------
abc   1.000000
def   2.000000

但我想删除 col1 和 col2,因为它出现在我不需要的渲染后页面中。

有什么办法可以做到吗?

【问题讨论】:

    标签: python-3.x pandas pandas-styles


    【解决方案1】:

    hide_columns 函数可用于隐藏列。类似于hide_index 函数对索引的作用:

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

    【讨论】:

      猜你喜欢
      • 2016-08-09
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2021-06-09
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      相关资源
      最近更新 更多