python divide value by 0
<p>I am trying to compare values in a table, it so happens that some might be zero and I therefore get an error message that I cannot divide by 0.
Why isn't the script returning inf instead of an error?
When I test this script on a dataframe with one column it works, with more than one column it breaks with the Zero Division Error.</p>

<pre><code>table[change] = ['{0}%'.format(str(round(100*x,2)) for x in \
                                         (table.ix[:,table.shape[1]-1] - table.ix[:,0]) / table.ix[:,0]]
</code></pre>

<p>table example:</p>

<pre><code>         0      1      2      3      4      5      6  \
numbers  0.0  100.0  120.0  220.0  250.0  300.0  500.0\\
revenues 50.0  100.0  120.0  220.0  250.0  300.0  500.0
</code></pre>

<p>where <code>table.ix[:,0]</code> is 0.0.</p>

<p>Some of the values at <code>table.ix[:,0]</code> are zero and others are not, hence, try and except in my experience will not work because the script will break once the value divisible is equal to 0.</p>

<p>I tried two of the other methods and they did not work for me.</p>

<p>Can you be a little more descriptive in your answer? I am struggling to take the approach given.</p>

<p>I have another approach which I am trying and it is not working. Do not see yet what the problem is:</p>

<pre><code>for index, row in table.iterrows():
    if row[0] == 0:
        table[change] = 'Nan'
    else:
        x = (row[-1] - row[0]) / row[0]
        table[change] = '{0} {1}%'.format( str(round(100 * x, 2)))
</code></pre>

<p>The 'change' column contains the same values (i.e. the last comparison of the table)</p>