【问题标题】:Empty column name in first column, how do I set the name?第一列中的空列名称,如何设置名称?
【发布时间】:2020-04-07 05:27:59
【问题描述】:

我有这个数据框

由于某种原因,当我执行此操作时 all_data = all_data.set_index(['Location+Type']) 名称“Location+Type”现在消失了。现在如何将数据框的第一列设置为名称“Location+Type”?

【问题讨论】:

  • all_data.index.name = 'Location+Type' 有帮助吗?
  • 如果调用set_index后没有显示索引名,则说明某处有bug或有问题。

标签: python pandas dataframe


【解决方案1】:

通过使用set_index(['Location+Type']),您不再拥有Location+Type 列,它现在是您的数据框的索引,因此在打印时,您看不到名称。如果您希望将Location+Type 恢复为列,则需要使用:

all_data = all_data.reset_index() 

这将创建一个新索引,同时将原始索引 (Location+Type) 返回到列。正如documentation 所说:

当我们重置索引时,旧索引作为列添加,并使用新的顺序索引:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 2012-12-14
    • 1970-01-01
    • 2015-05-24
    • 2022-01-13
    • 2021-08-30
    • 2017-06-11
    • 2021-09-30
    相关资源
    最近更新 更多