划分两只大熊猫系列

时间:2017-01-18 17:58:25

标签: pandas

尝试划分2个系列,但我得到的行为我不明白

a = 14    0.27
    15    0.11
    16    0.00
    dtype: float64

a.index返回

Int64Index([14, 15, 16], dtype='int64')

b = 14    0.150286
    15    0.108026
    16    0.000000
    dtype: float64

b.index返回

Index([u'14', u'15', u'16'], dtype='object')

当我做的时候

a.divide(b) or a/b

我得到相同的结果

14   NaN
15   NaN
16   NaN
14   NaN
15   NaN
16   NaN

这应该很简单,但我不明白为什么要返回系列而不是返回预期的

   14   1.7965
   15   1.0182
   16   NaN

1 个答案:

答案 0 :(得分:5)

我认为dtypesindexes个不同,所以需要相同类型 - 例如将object(显然str)投射到int

a = pd.Series([0.27, 0.11, 0], index=['14','15','16'])
b = pd.Series([0.150286, 0.108026, 0], index=[14,15,16])
print (a)
14    0.27
15    0.11
16    0.00
dtype: float64

print (b)
14    0.150286
15    0.108026
16    0.000000
dtype: float64

print (a.index.dtype)
object
print (b.index.dtype)
int64

#cast to int
a.index = a.index.astype(int)
print (a.div(b))
14    1.796575
15    1.018273
16         NaN
dtype: float64
相关问题