我有以下数据:
a = pd.Series([1, 2, 3])
b = pd.Series([0, 0, 0])
如果有被零除的情况,我想在某些情况下
- 将结果设置为系列之一
- 将结果设置为特定值
但以下给出了“意外”结果:
a.div(b, fill_value = 0)
0 inf
1 inf
2 inf
a.div(b).fillna(0)
0 inf
1 inf
2 inf
a.div(b).combine_first(a)
0 inf
1 inf
2 inf
我想要到达:
case 1:将数据设置为特定值
0 0
1 0
2 0
case 2:将值设置为特定系列
0 1
1 2
2 3
您可以使用df.replace
划分后:
(a / b).replace(np.inf, 0)
0 0.0
1 0.0
2 0.0
dtype: float64
(a / b).replace(np.inf, a)
0 1.0
1 2.0
2 3.0
dtype: float64
也想处理负无穷大吗?你需要:
(a / b).replace((np.inf, -np.inf), (a, a))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)