Search code examples
pythonpandasmulti-index

Reindex a series output doesn't match pandas example in the documentation


I have a series like the following.

In [53]: aStack
Out[53]: 
Age_cat  S0102_gender
_1       Male            167.047843
         Female          211.292291
         All             378.340134
_2       Male            132.055426
         Female          171.780338
         All             303.835764
All      Male            299.103269
         Female          383.072629
         All             682.175898
dtype: float64

In [54]: 

I would like to convert the series to something that looks like this.

In [53]: aStack
Out[53]: 
Age_cat  S0102_gender
_1       Male            167.047843
_1       Female          211.292291
_2       All             378.340134
_2       Male            132.055426
_2       Female          171.780338
_2       All             303.835764
All      Male            299.103269
All      Female          383.072629
All      All             682.175898

I have a list of tuples.

combos
Out[54]: 
[('_1', 'Male'),
 ('_1', 'Female'),
 ('_1', 'All'),
 ('_2', 'Male'),
 ('_2', 'Female'),
 ('_2', 'All'),
 ('All', 'Male'),
 ('All', 'Female'),
 ('All', 'All')]

According to the documentation. If I run,

aStack.reindex(combos)

I should get that output. However, when I run the instruction, I get the same output as my input. What am I missing?


Solution

  • Your starting series already has a MultiIndex, which does not print the outermost (level 0) index values on each row. To see each index value repeated down in each row (for display purposes, etc.), try aStack.reset_index().