Home > Software engineering >  Zero Division in one row causing error in all other rows Pandas
Zero Division in one row causing error in all other rows Pandas

Time:10-08

Considering the two columns of this df:

    ColA        ColB    
0   0.0         0.0     
1   5.523288    0.0     
2   6.115068    0.0     
3   6.90411     1.0     
4   9.172603    2.0     
5   10.849315   2.0     
6   12.230137   0.0     
7   11.210959   0.0     
8   10.849315   1.0     
9   2.169863    0.0     

Attempting to generate a calculated column in this way:

df['Result'] = df['ColB']/df['ColA']

This attempt raises 'float division by zero' error as expected because of the calculation in the first row being a division by zero. Saw this and then did this to navigate around that temporarily:

try:
     df['Result'] = df['ColB']/df['ColA']
except ZeroDivisionError:
     df['Result'] = 0

However, this code consistently producing this result (i.e. all rows are zeros)

        ColA        ColB   Result    
    0   0.0         0.0     0
    1   5.523288    0.0     0
    2   6.115068    0.0     0
    3   6.90411     1.0     0
    4   9.172603    2.0     0
    5   10.849315   2.0     0
    6   12.230137   0.0     0
    7   11.210959   0.0     0
    8   10.849315   1.0     0
    9   2.169863    0.0     0

Starting at Index Row 3, the Result column should be producing float values that are not merely zero. I also inserted "some error" in the above try except and all the values in the Result column displayed "some error."

I am at a loss as to why pandas is not bypassing the error and producing valid results in the appropriate rows.

CodePudding user response:

Try this -

df['Result'] = (df['ColB']/df['ColA']).fillna(0)
        ColA  ColB    Result
0   0.000000   0.0  0.000000
1   5.523288   0.0  0.000000
2   6.115068   0.0  0.000000
3   6.904110   1.0  0.144841
4   9.172603   2.0  0.218041
5  10.849315   2.0  0.184343
6  12.230137   0.0  0.000000
7  11.210959   0.0  0.000000
8  10.849315   1.0  0.092172
9   2.169863   0.0  0.000000

Check out the documentation here


Regarding this -

try:
     df['Result'] = df['ColB']/df['ColA']
except ZeroDivisionError:
     df['Result'] = 0

I am actually no able to reproduce the result that you are facing. Here is what I get as expected.

        ColA  ColB    Result
0   0.000000   0.0       NaN #<- 0/0 still throws nan
1   5.523288   0.0  0.000000 #<- divide by 0 
2   6.115068   0.0  0.000000
3   6.904110   1.0  0.144841
4   9.172603   2.0  0.218041
5  10.849315   2.0  0.184343
6  12.230137   0.0  0.000000 <- divide by 0
7  11.210959   0.0  0.000000
8  10.849315   1.0  0.092172
9   2.169863   0.0  0.000000
  • Related