【问题标题】:Convert index to pandas column将索引转换为 pandas 列
【发布时间】:2019-05-04 13:38:56
【问题描述】:

我有一个包含两个索引和一个列的数据集,如下所示:

                         value
cod      Date               
A        2011-01-01  5.540642
         2011-02-01  5.556837
         2011-03-01  5.565547
         2011-04-01  5.557245
         2011-05-01  5.604736
         2011-06-01  5.570242
         2011-07-01  5.578222
         2011-08-01  5.826457
         2011-09-01  6.391667
         2011-10-01  6.880779
         2011-11-01  5.610012
         2011-12-01  5.572432

我想在 pandas 列中转换索引。当我使用以下代码时:df.reset_index(inplace=True) 它会做我想做的事。但如果我尝试将其放入另一个对象中,以创建一个新数据集:df1 = df.reset_index(inplace=True),该函数会给我一个空对象,确切地说是 NoneType 对象。

【问题讨论】:

    标签: python-3.x pandas indexing


    【解决方案1】:

    DataFrame.reset_index中的inplace=True需要去掉inplace=False的默认参数,所以应该省略。

    就地:布尔值,默认为 False

    就地修改DataFrame(不要创建新对象)

    df1 = df.reset_index()
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2016-09-26
      • 1970-01-01
      • 2021-11-30
      • 2021-05-03
      • 2020-03-24
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多