添加两个带有NaN的序列 [英] Adding two Series with NaNs
问题描述
我正在通过"Python进行数据分析",但我不了解特定的功能.添加两个熊猫系列对象将自动对齐索引数据,但是如果一个对象不包含该索引,则将其返回为NaN.例如书中的内容:
I'm working through the "Python For Data Analysis" and I don't understand a particular functionality. Adding two pandas series objects will automatically align the indexed data but if one object does not contain that index it is returned as NaN. For example from book:
a = Series([35000,71000,16000,5000],index=['Ohio','Texas','Oregon','Utah'])
b = Series([NaN,71000,16000,35000],index=['California', 'Texas', 'Oregon', 'Ohio'])
结果:
In [63]: a
Out[63]: Ohio 35000
Texas 71000
Oregon 16000
Utah 5000
In [64]: b
Out[64]: California NaN
Texas 71000
Oregon 16000
Ohio 35000
当我将它们加在一起时,我得到了...
When I add them together I get this...
In [65]: a+b
Out[65]: California NaN
Ohio 70000
Oregon 32000
Texas 142000
Utah NaN
那么,为什么犹他州的价值是NaN而不是500?看来500 + NaN = 500.是什么赋予了?我丢失了一些东西,请解释.
So why is the Utah value NaN and not 500? It seems that 500+NaN=500. What gives? I'm missing something, please explain.
更新:
In [92]: # fill NaN with zero
b = b.fillna(0)
b
Out[92]: California 0
Texas 71000
Oregon 16000
Ohio 35000
In [93]: a
Out[93]: Ohio 35000
Texas 71000
Oregon 16000
Utah 5000
In [94]: # a is still good
a+b
Out[94]: California NaN
Ohio 70000
Oregon 32000
Texas 142000
Utah NaN
推荐答案
Pandas不假定500 + NaN = 500,但是很容易要求它这样做:a.add(b, fill_value=0)
Pandas does not assume that 500+NaN=500, but it is easy to ask it to do that: a.add(b, fill_value=0)
这篇关于添加两个带有NaN的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!