3

The following code returns a series with NaNs everywhere:

s = pd.Series([1, 2, 3, 4, 5, 6],
                 index=pd.MultiIndex.from_product([["A", "B"], ["c", "d", "e"]]))

s.reindex([('E', 'g'), ('E', 'h'), ('E', 'i'), ('F', 'g'), ('F', 'h'), ('F', 'i')])

or

s.reindex(pd.MultiIndex.from_product([['E', 'F'], ['g', 'h', 'i']]))

How can I reindex the series and retain the original values?

halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140

2 Answers2

3

This is not reindex , that is change the index

s.index=pd.MultiIndex.from_product([['E', 'F'], ['g', 'h', 'i']])
s
Out[362]: 
E  g    1
   h    2
   i    3
F  g    4
   h    5
   i    6
dtype: int64
BENY
  • 317,841
  • 20
  • 164
  • 234
1

If need set new values to second level use MultiIndex.set_levels:

s.index = s.index.set_levels(['g', 'h', 'i'], level=1)
print (s)
A  g    1
   h    2
   i    3
B  g    4
   h    5
   i    6
dtype: int64
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252