【问题标题】:Adding index level to a dataframe将索引级别添加到数据框
【发布时间】:2014-03-22 00:24:29
【问题描述】:

我有一个数据框,其中一个索引为日期时间,如下所示,我希望添加一个第一列索引(参见下面的“目标”),其中任何日期都与它交叉(First_column)。

First_column = ['s0000', 's0001', 's0002', 's0003', 's0004', ...]

有人知道如何进行吗?

非常感谢。 亚历克西斯

我的数据框:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 17544 entries, 2015-01-01 00:00:00 to 2016-12-31 23:00:00
Data columns (total 12 columns):

目标:

<class 'pandas.core.frame.DataFrame'>
MultiIndex: 996000 entries, (s0000, 2015-01-01 00:00:00) to (s0999, 2012-12-31 00:00:00)
Data columns (total 8 columns):

场景日期

s0000    2015-02-28
         2015-03-03 
         2015-03-04
         2015-03-05
         2015-03-06
         2015-03-07
         2015-03-10
         2015-03-11
         2015-03-12
         2015-03-13
s0001    2015-02-28
         2015-03-03 
         2015-03-04
         2015-03-05
         2015-03-06
         2015-03-07
         2015-03-10
         2015-03-11
         2015-03-12
         2015-03-13
s0002    2015-02-28
         2015-03-03 
         2015-03-04
         2015-03-05
         2015-03-06
         2015-03-07
         2015-03-10
         2015-03-11
         2015-03-12
         2015-03-13
s0003    ...

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以将pd.concatkeys 参数一起使用:

    import pandas as pd
    df = pd.DataFrame(range(10), index=pd.date_range('2015-2-27', freq='B', periods=10))
    #             0
    # 2015-02-27  0
    # 2015-03-02  1
    # 2015-03-03  2
    # 2015-03-04  3
    # 2015-03-05  4
    # 2015-03-06  5
    # 2015-03-09  6
    # 2015-03-10  7
    # 2015-03-11  8
    # 2015-03-12  9
    first_col = ['s{:04d}'.format(i) for i in range(1,5)]
    # ['s0001d', 's0002d', 's0003d', 's0004d']
    
    newdf = pd.concat([df]*len(first_col), keys=first_col)
    print(newdf)
    

    产量

                      0
    s0001 2015-02-27  0
          2015-03-02  1
          2015-03-03  2
          2015-03-04  3
          2015-03-05  4
          2015-03-06  5
          2015-03-09  6
          2015-03-10  7
          2015-03-11  8
          2015-03-12  9
    s0002 2015-02-27  0
          2015-03-02  1
          2015-03-03  2
          2015-03-04  3
          2015-03-05  4
          2015-03-06  5
          2015-03-09  6
          2015-03-10  7
          2015-03-11  8
          2015-03-12  9
    s0003 2015-02-27  0
          2015-03-02  1
          2015-03-03  2
          2015-03-04  3
          2015-03-05  4
          2015-03-06  5
          2015-03-09  6
          2015-03-10  7
          2015-03-11  8
          2015-03-12  9
    s0004 2015-02-27  0
          2015-03-02  1
          2015-03-03  2
          2015-03-04  3
          2015-03-05  4
          2015-03-06  5
          2015-03-09  6
          2015-03-10  7
          2015-03-11  8
          2015-03-12  9
    

    很高兴,我刚刚学到了这个yesterday from Joris

    【讨论】:

      【解决方案2】:

      你可以做这样的事情......

      import pandas as pd
      
      first_col = ['s0001', 's0002', 's0003', 's0004']
      
      # Make your datetime index
      dt_index = pd.date_range('2015-2-27', freq='B', periods=10)
      
      # Make your first_col index - must be same length as dt_index 
      first_col_index = len(dt_index)*first_col
      first_col_index.sort()
      
      # Make a dateframe with a hierarchical index
      df = pd.DataFrame(range(len(first_col)*len(dt_index)), index=[first_col_index,
                        dt_index.repeat(len(first_col))])
      

      【讨论】:

        猜你喜欢
        • 2016-09-17
        • 2019-02-28
        • 2019-01-16
        • 1970-01-01
        • 1970-01-01
        • 2018-12-20
        • 2021-09-27
        • 2018-04-20
        • 1970-01-01
        相关资源
        最近更新 更多