0

I have to calculate a column based on two other ones, but if the ZeroDivisionError's exception is raised, a value of 0 will be assigned to the concerned rows.

I've already tried this one:

try:
    df['Taux_rebut'] = df['Rebut_mois'] / df['Brute_mois']
except ZeroDivisionError:
    df['Taux_rebut'] = 0

The main problem is that whenever the exception is raised, the df['Taux_rebut'] = 0 code is executed, then the whole column is assigned a value of 0.

What I want is to assign 0 to the only rows where the exception is raised, and to perform the calculation on the other ones.

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
zedkaria
  • 1
  • 1
  • 1

0 Answers0