I have the following data:
a = pd.Series([1, 2, 3]) b = pd.Series([0, 0, 0])
If there is a division by zero I want to in some cases
You can use df.replace after division:
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
Want to handle negative infinity too? You'll need:
(a / b).replace((np.inf, -np.inf), (a, a))