【问题标题】:Pandas Add Column Index Level to Data FramePandas 将列索引级别添加到数据框中
【发布时间】:2016-09-17 04:50:46
【问题描述】:

给定以下数据框:

d2=pd.DataFrame({'Item':['y','y','z','x'],
                'other':['aa','bb','cc','dd']})
d2

    Item    other
0   y       aa
1   y       bb
2   z       cc
3   x       dd

我想在现有的(我认为)下添加一个列索引级别 1,因为我想将此数据框连接到另一个是多索引的数据框。 我不想更改其他数据框,因为我已经编写了很多假设其当前结构的代码。

提前致谢!

【问题讨论】:

    标签: python-3.x pandas multi-index


    【解决方案1】:

    IIUC 你可以将参数append=True 添加到set_index:

    print (d2.set_index('Item', append=True))
           other
      Item      
    0 y       aa
    1 y       bb
    2 z       cc
    3 x       dd
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 2014-05-02
      • 2015-08-11
      • 2020-12-13
      • 1970-01-01
      相关资源
      最近更新 更多