简体   繁体   English

重新索引Series时,值将变为NaN

[英]While reindexing a Series , values are changing into NaN's

import numpy as np
from pandas import DataFrame,Series
n = np.arange(0,5)
s = Series(n,index =['a','b','c','d','e'])
print(s)
s= s.reindex(['A','B','C','D','E'])
print(s)

Output: 输出:

a    0
b    1
c    2
d    3
e    4
dtype: int64
A   NaN
B   NaN
C   NaN
D   NaN
E   NaN
dtype: float64

Use the script below. 使用下面的脚本。 It will change the value of the index to your desired values. 它将把索引的值更改为您想要的值。 Reindex will not change the values but will change the order of the records and if such index value is not found, it will put NaN. 重新索引不会更改值,但会更改记录的顺序,如果找不到该索引值,它将放入NaN。

OLD: 旧:

s= s.reindex(['A','B','C','D','E'])

New: 新:

s.index=['A','B','C','D','E']

Result: 结果:

A    0
B    1
C    2
D    3
E    4
dtype: int64

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

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