'Divide two pandas Series
trying to divide 2 series but im getting a behavior i dont understand
a = 14 0.27
15 0.11
16 0.00
dtype: float64
a.index returns
Int64Index([14, 15, 16], dtype='int64')
and
b = 14 0.150286
15 0.108026
16 0.000000
dtype: float64
b.index returns
Index([u'14', u'15', u'16'], dtype='object')
When i do
a.divide(b) or a/b
i get the same result
14 NaN
15 NaN
16 NaN
14 NaN
15 NaN
16 NaN
this should be pretty simple but i dont understand why is returning the series instead of returning the expected
14 1.7965
15 1.0182
16 NaN
Solution 1:[1]
I think there are different dtypes of indexes, so need same type - e.g. cast object (obviously str) to 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
Solution 2:[2]
Switching to numpy arrays and getting back to a pandas series afterwards can also work:
a = pd.Series([0.27, 0.11, 0], index=['14','15','16'])
b = pd.Series([0.150286, 0.108026, 0], index=[14,15,16])
# the index to keep
my_index = a.index()
c = a.values / b.values
c = pd.Series(c, index=my_index)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Imrane |
