Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

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. I first tried to do the following code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

One idea with ordered categoricals in MultiIndex.set_levels, so possible use 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:

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading