我正在通過「Python for Data Analysis」工作,但我不明白某個特定的功能。添加兩個pandas系列對象將自動對齊索引數據,但如果一個對象不包含該索引,則它將返回爲NaN。例如從書:添加兩個pandas.series對象
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
當我把它們加起來我得到這個...
In [65]: a+b
Out[65]: California NaN
Ohio 70000
Oregon 32000
Texas 142000
Utah NaN
那麼爲什麼猶他NaN值,而不是500?看來500 + NaN = 500。是什麼賦予了?我錯過了一些東西,請解釋一下。
更新:
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
解決:'+'操作符執行兩者的聯合。我需要.add()方法。 – joelotz 2013-04-24 22:20:54