Search code examples
pythonpandasdataframedivide-by-zero

How to get rid of Divide by Zero error in python pandas?


I have a dataframe that has two columns:

df

enter image description here

There are some scientific values like 0E-10, I want create a new column such that:

df["new_col"]=df["fruits_ratio"]/(df["fruits_ratio"]+df["vegetables_ratio"])

But it give me an error like this: DivisionByZero: [<class 'decimal.DivisionByZero'>]

Is there a way to either replace the 0-E10 values or how can I avoid the error?

Thanks


Solution

  • I think the float function will fix this for you float(). Assign the entire column as floats since most are already.