pandas Python Reindex 生成 Nan
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35108046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Reindex Producing Nan
提问by Adam Warner
Here is the code that I am working with:
这是我正在使用的代码:
import pandas as pd
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:
所以最初一切都很好,test3 的索引为 'a' 'b' 'c' 并且值为 1,2,3。但是当我重新索引 test3 时,我发现我的值 1 2 3 丢失了。这是为什么?所需的输出是:
f 1
g 2
z 3
采纳答案by EdChum
The docsare 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
通过可选的填充逻辑使 Series 符合新索引,将 NA/NaN 放置在先前索引中没有值的位置
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