简体   繁体   English

如何在多个级别上重新索引 MultiIndex Dataframe?

[英]How to Reindex MultiIndex Dataframe on Multiple Levels?

So I currently have the following dataframe which I aggregated and I have a multiindex that looks like the following:因此,我目前有以下 dataframe 汇总,并且我有一个如下所示的多索引:

Date     Country_Band      Value      Decimal
May 2021 Non-US            2-14       0.11
         US                2-14       0.22
                           1          0.33
                           15+        0.44
         Non-US            1          0.55
                           15+        0.66

I want to organize and group these in a way to obtain the below:我想以某种方式组织和分组这些以获得以下内容:

Date     Country_Band      Value      Decimal
May 2021 US                1          0.33
                           2-14       0.22
                           15+        0.44

         Non-US            1          0.55
                           2-14       0.11
                           15+        0.66

This is the index of a larger dataframe.这是一个更大的 dataframe 的索引。 I first tried to do the following code:我首先尝试执行以下代码:

df_march_agg = df_march_agg.reindex(['US', 'Non-US'], level='Country_Band')

Which worked in getting the country band group, however, the value is still not in numerical order:这有助于获得国家乐队组,但是,该值仍然不是数字顺序:

Date     Country_Band      Value      Decimal
May 2021 US                2-14       0.22
                           1          0.33 
                           15+        0.44

         Non-US            2-14       0.11
                           1          0.55
                           15+        0.66

I tried then doing the same:我试着做同样的事情:

df_march_agg = df_march_agg.reindex(['1', '2-14', '15+'], level='Value')

But this then undid the previous reindex.但这随后取消了之前的重新索引。 Any idea of what I am missing or need to add in order to get both in order?知道我缺少什么或需要添加什么才能使两者都井井有条吗?

Cheers!干杯!

One idea with ordered categoricals in MultiIndex.set_levels , so possible use DataFrame.sort_index :MultiIndex.set_levels中使用有序分类的一个想法,因此可以使用DataFrame.sort_index

df.index = (df.index.set_levels(pd.CategoricalIndex(df.index.levels[1], 
                                                   ordered=True,
                                                   categories=['US', 'Non-US']), 
                                                   level=1)
                    .set_levels(pd.CategoricalIndex(df.index.levels[2], 
                                                   ordered=True, 
                                                   categories=['1', '2-14', '15+']), 
                                                   level=2))

df = df.sort_index()
print (df)
                             Decimal
Date     Country_Band Value         
May 2021 US           1         0.33
                      2-14      0.22
                      15+       0.44
         Non-US       1         0.55
                      2-14      0.11
                      15+       0.66

Another idea with DataFrame.reindex with MultiIndex.from_product : DataFrame.reindexMultiIndex.from_product的另一个想法:

mux = pd.MultiIndex.from_product([['May 2021'],
                                  ['US', 'Non-US'],
                                  ['1', '2-14', '15+']], 
                                  names=['Date','Country_Band','Value'])

df = df.reindex(mux)
print (df)
                             Decimal
Date     Country_Band Value         
May 2021 US           1         0.33
                      2-14      0.22
                      15+       0.44
         Non-US       1         0.55
                      2-14      0.11
                      15+       0.66

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM