【问题标题】:How can I set a label for the index column? [duplicate]如何为索引列设置标签? [复制]
【发布时间】:2021-10-27 20:31:31
【问题描述】:

我有一个数据集,我正在使用 reset_index 来重置索引:

output = output.reset_index(drop=True)

这可以正常工作,但我最终得到一个没有标签的索引列。换句话说,所有其他列都设置了标签,除了电子表格中的第一列。我希望该索引列具有“索引”标签。

谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    试试rename_axis

    output = output.rename_axis('Index')
    

    【讨论】:

    • 这对我有用,谢谢!这就是我使用它的方式: output = output.reset_index(drop=True).rename_axis('Index')
    【解决方案2】:

    您可以指定名称参数:

    output = output.reset_index(name='Index')
    

    output = output.reset_index(name='Index', drop=True)
    

    【讨论】:

    【解决方案3】:

    索引标签是它的属性。你可以通过赋值来改变它:

    output.index.name = "Index"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 2018-06-11
      • 2019-01-17
      相关资源
      最近更新 更多