【问题标题】:Create a multiIndex with pandas使用 pandas 创建一个 multiIndex
【发布时间】:2020-05-17 12:26:21
【问题描述】:

我有一个带有两个索引的数据框,如下所示:

Index1       Index2             200701     200702     200703      
alphas       Fourth Quartile    41.7421    41.1807     39.071           
             Third Quartile     74.1573    95.0195    90.6572          
             Second Quartile   -34.2001   -42.0068   -21.6236  
             First Quartile      39.293    37.3475    34.1704        
             All_Quartiles      37.6624    38.5957    38.0504        
betas        Fourth Quartile    18.1041    23.0865    33.7109       
             Third Quartile    -51.9743   -93.1191   -87.1772        
             Second Quartile    121.262    131.556    103.549        
             First Quartile     26.1859    28.5129    31.8663          
             All_Quartiles       24.511    23.1601    0.159067  

我需要新的索引,像这样:

New_index  Index1     Index 2            200701     200702     200703      
Sector     alphas     Fourth Quartile    41.7421    41.1807     39.071              
                      Third Quartile     74.1573    95.0195    90.6572         
                      Second Quartile   -34.2001   -42.0068   -21.6236      
                      First Quartile      39.293    37.3475    34.1704        
                      All_Quartiles      37.6624    38.5957    38.0504     
           betas      Fourth Quartile    18.1041    23.0865    33.7109       
                      Third Quartile    -51.9743   -93.1191   -87.1772          
                      Second Quartile    121.262    131.556    103.549            
                      First Quartile     26.1859    28.5129    31.8663          
                      All_Quartiles       24.511    23.1601    0.159067     

我有许多数据帧多索引属于不同的部门,我需要用一个循环合并每个数据帧。

【问题讨论】:

  • 您真的需要为单个 DataFrame 执行此操作,还是仅在组合它们时添加 'New_index' 级别就足够了,这似乎是最自然的?如果是后者,您正在寻找 pd.concatkeys 参数。
  • 不,我不能使用pd.concat 和参数keys 我需要使用df.append 但首先我需要创建新索引

标签: python pandas dataframe concatenation multi-index


【解决方案1】:

您可以手动重新创建整个 MultiIndex,但这需要大量的写作。我更喜欢 concatkeys 参数来添加额外的级别。 names 参数允许我们给它一个名字。

pd.concat([df], keys=['Sector'], names=['New_index']+df.index.names)

                                    200701    200702      200703
New_index Index1 Index2                                         
Sector    alphas Fourth Quartile   41.7421   41.1807   39.071000
                 Third Quartile    74.1573   95.0195   90.657200
                 Second Quartile  -34.2001  -42.0068  -21.623600
                 First Quartile    39.2930   37.3475   34.170400
                 All_Quartiles     37.6624   38.5957   38.050400
          betas  Fourth Quartile   18.1041   23.0865   33.710900
                 Third Quartile   -51.9743  -93.1191  -87.177200
                 Second Quartile  121.2620  131.5560  103.549000
                 First Quartile    26.1859   28.5129   31.866300
                 All_Quartiles     24.5110   23.1601    0.159067

这里将与手动重新创建 MultiIndex 相同。

arrays = []

arrays.append(pd.Index(['Sector']*len(df), name='New_Index')) # 0th level sector

# Add all existing levels
for i in range(df.index.nlevels):
    arrays.append(df.index.get_level_values(i))

new_idx = pd.MultiIndex.from_arrays(arrays)

df.index = new_idx

上面基本上是DataFrame.set_index(append=True) 的内部结构,所以你可以用它来清理一下。

df['New_index'] = 'Sector'                  # New column
df = df.set_index('New_index', append=True) # Bring it to index
df = df.reorder_levels([2, 0, 1])           # Move it to the front

【讨论】:

  • 或者将方法链在一起一步完成....df.assign(New_index='Sector').set_index('New_index', append=True).reorder_levels([2,0,1])
  • 感谢您的帮助,我的目的达到了。
猜你喜欢
  • 2015-04-02
  • 2019-06-13
  • 2016-11-24
  • 2021-06-14
  • 2019-08-27
  • 1970-01-01
  • 2022-12-07
  • 2021-01-29
  • 2021-12-13
相关资源
最近更新 更多