Python Reindex Producing Nan

Question:

Here is the code that I am working with:

import pandas as pd

test3 = pd.Series([1,2,3], index = ['a','b','c'])
test3 = test3.reindex(index = ['f','g','z'])

So originally every thing is fine and test3 has an index of ‘a’ ‘b’ ‘c’ and values 1,2,3. But then when I got to reindex test3 I get that my values 1 2 3 are lost. Why is that? The desired output would be:

f 1
g 2
z 3
Asked By: Adam Warner

||

Answers:

The docs are clear on this behaviour :

Conform Series to new index with optional filling logic, placing
NA/NaN in locations having no value in the previous index

if you just want to overwrite the index values then do:

In [32]:
test3.index  = ['f','g','z']

test3
Out[32]:
f    1
g    2
z    3
dtype: int64
Answered By: EdChum
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.