【问题标题】:How to add a title to a pandas dataframe如何为熊猫数据框添加标题
【发布时间】:2020-09-15 16:21:03
【问题描述】:

我一直在深入研究pandas styler 选项,并在我的 df 中添加了图像、条件格式和标题。现在,我想用一个标题来结束它。我似乎无法弄清楚如何做到这一点。在样式文档中,它给出的示例将 df 转换为 HTML 表格,以便给它一个标题,但我不想这样做。 This SO question 帮我添加了标题,但没有添加标题。有没有办法做到这一点?谢谢!

styles = [
    dict(selector="caption", props=[("caption-side", "bottom")]),
    dict(selector='th', props=[('text-align', 'center')])
]

这是我的一些样式代码。 “标题”有选择器吗?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    使用 pandas.DataFrame.index 访问 DataFrame 的索引。为索引的 name 属性分配一个标题。

    df = pd.DataFrame({"Letters": ["a", "b", "c"], "Numbers": [1, 2, 3]})
    index = df.index
    index.name = "Index Title"
    
    print(df)
    

    【讨论】:

    • 我想要整个数据框的标题,而不仅仅是索引。
    猜你喜欢
    • 1970-01-01
    • 2019-06-16
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多