【问题标题】:While reindexing a Series , values are changing into NaN's在重新索引 Series 时,值正在变为 NaN
【发布时间】:2019-07-16 03:00:05
【问题描述】:
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)

输出:

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

【问题讨论】:

  • 正确格式化您的代码

标签: python


【解决方案1】:

使用下面的脚本。它会将索引的值更改为您想要的值。重新索引不会改变值,但会改变记录的顺序,如果没有找到这样的索引值,它会放 NaN。

旧:

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

新:

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

结果:

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

【讨论】:

猜你喜欢
  • 2023-01-02
  • 2013-07-17
  • 2022-08-22
  • 2018-08-22
  • 2020-09-01
  • 2020-06-15
  • 1970-01-01
  • 2017-05-07
  • 2017-02-26
相关资源
最近更新 更多