So I wrote a code that divides a column by another column:
df['division'] = df['numerator']/df['denominator']
But since the 'denominator' is a result of other calculations, sometimes it is equal to 0. When that is the case, of course we get errors. To prevent that I used try and except like so:
Try:
df['division'] = df['numerator']/df['denominator']
Except:
df['division'] = 0
BUT I think that this doesn't work as I thought it would, since if we get any exceptions ALL the values will be 0 in the division column.
I solved it with:
np.where(df['denominator'] !=0, do_the_thing, 0)
But I wonder if 0 was not the only cause of an exception, what would I do? Is there an efficient way to check exceptions in line by line operations?
Thanks in advance.