【问题标题】:How change index in multiindex dataframe如何更改多索引数据框中的索引
【发布时间】:2023-03-14 03:55:01
【问题描述】:

我有一个多索引熊猫数据框,例如:

            col1        col2        col3        col4        col5
ix  iy                                                        
0   14       True          1           1          1           1
    25       True          1           1          1           1
    27       True          1           1          0           1
    28       True          1           1          1           0
    43       True          1           1          1           1
1   12       True          1           1          1           1
    38       True          1           1          1           1
2   0        True          1           1          0           1
    1        True          1           1          1           0

如何通过向它们添加常量值来更改索引?例如ix + 5iy + 10

            col1        col2        col3        col4        col5
ix  iy                                                        
5   24       True          1           1          1           1
    35       True          1           1          1           1
    37       True          1           1          0           1
    38       True          1           1          1           0
    53       True          1           1          1           1
6   22       True          1           1          1           1
    48       True          1           1          1           1
7   10       True          1           1          0           1
    11       True          1           1          1           0 

【问题讨论】:

    标签: pandas dataframe indexing multi-index


    【解决方案1】:

    您可以使用元组和MultiIndex.from_tuples 重新创建新的MultiIndex

    L = [(a + 5, b + 10) for a, b in df.index]
    df = df.set_index(pd.MultiIndex.from_tuples(L, names=df.index.names))
    
    print (df)
          col1  col2  col3  col4  col5
    ix iy                              
    5  24  True     1     1     1     1
       35  True     1     1     1     1
       37  True     1     1     0     1
       38  True     1     1     1     0
       53  True     1     1     1     1
    6  22  True     1     1     1     1
       48  True     1     1     1     1
    7  10  True     1     1     0     1
       11  True     1     1     1     0
    

    MultiIndex.set_levels 的另一个想法:

    df.index = df.index.set_levels(df.index.levels[0] + 5, level=0)
    df.index = df.index.set_levels(df.index.levels[1] + 10, level=1)
    print (df)
           col1  col2  col3  col4  col5
    ix iy                              
    5  24  True     1     1     1     1
       35  True     1     1     1     1
       37  True     1     1     0     1
       38  True     1     1     1     0
       53  True     1     1     1     1
    6  22  True     1     1     1     1
       48  True     1     1     1     1
    7  10  True     1     1     0     1
       11  True     1     1     1     0
    

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多