2017-01-18 6 views
1

は、2シリーズを分割しようとしているが、イムは、私は分割2頭のパンダはシリーズ

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

b = 14 0.150286 
    15 0.108026 
    16 0.000000 
    dtype: float64 

b.index戻り

a = 14 0.27 
    15 0.11 
    16 0.00 
    dtype: float64 

a.indexリターンを理解しない振る舞いを取得

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

私は

a.divide(b) or a/b 

iが同じ結果を得る行うと

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

これは非常に単純でなければなりませんが、私は、なぜシリーズを返す代わりに期待

14 1.7965 
    15 1.0182 
    16 NaN 
を返して理解しません
+1

1つの文字列と別のintで、インデックスの異なる種類があるようです。 – jezrael

+0

同じインデックスであれば、 'a.divide(b.values)'を使うことができます – jezrael

+0

両方のシリーズは 'float64'型です@jezrael –

答えて

2

dtypesindexesだと思いますので、同じタイプが必要です - 例えばintに(明らかにstrobjectをキャスト:

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 
+0

私は非常に混乱していたので、私はなぜそんなに単純なもののような結果を得ていたのか理解できませんでした –

+0

一度2時間は見つかりませんでしたこの問題... – jezrael

関連する問題