加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

pandas series使用reindex 返回Nan

发布时间:2020-12-17 17:02:36 所属栏目:Python 来源:网络整理
导读:在pandas使用reindex时,发现新的索引返回的值都是Nan,结果查阅文档发现原来是自己方法用错了,我原本想的是替换索引,而reindex作用是填充索引。 如: import?pandas?as?pdtest?=?pd.Series([1,2,3],?index?=?['a','b','c'])test?=?test?.reindex(index?=?

在pandas使用reindex时,发现新的索引返回的值都是Nan,结果查阅文档发现原来是自己方法用错了,我原本想的是替换索引,而reindex作用是填充索引。

如:

import?pandas?as?pd
test?=?pd.Series([1,2,3],?index?=?['a','b','c'])
test?=?test?.reindex(index?=?['f','g','z'])

#?打印结果
f?Nan
g?Nan
z?Nan

#?我想要的结果
f?1
g?2
z?3

文档解释:

Conform Series to new index with optional filling logic.

Places NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and?copy=False.

中文意思:

把一个新的可选的填充逻辑索引整合进Series,将NA / NaN放在上一个索引中没有值的位置。 除非新索引等于当前索引并且copy = False,否则将生成一个新对象。

解决方法:

import?pandas?as?pd
test?=?pd.Series([1,'c'])
test.index?=?['f','z']

##?输出结果
f????1
g????2
z????3
dtype:?int64


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读