首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与熊猫一起创建一个multiIndex

与熊猫一起创建一个multiIndex
EN

Stack Overflow用户
提问于 2020-01-31 20:10:15
回答 1查看 42关注 0票数 1

我有一个有两个索引的dataframe:

代码语言:javascript
复制
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  

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

代码语言:javascript
复制
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     

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-31 20:34:11

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

代码语言:javascript
复制
pd.concat([df], keys=['Sector'], names=['New_index']+df.index.names)

代码语言:javascript
复制
                                    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。

代码语言:javascript
复制
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)的内部结构,所以您可以用它来稍微清理一下。

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60010583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档