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.