Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python / Pandas getting division by zero

I’m trying to divide but get a "ZeroDivisionError: division by zero" despite I check for 0 values before the calculation:

df['var'] = None
df.loc[(df['use'] == 1) & (df['cont_var'] != 0) , 'var'] = (df['par1'] + df['par2'] + df['par3'] + df['par4']) / df['cont_var']

If I check all unique values for that condition, there is nothing wrong:

df[(df['use'] == 1) & (df['cont_var'] != 0)]['cont_var'].unique() 

I get: array([4, 2, 1], dtype=object)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m using Python 3.9.12 in Jupyter Notebook.

>Solution :

You can filter column cont_var used for division, also is used sum for simplify solution:

data = {
    'use': [1,1,1,1,1,0,0,0],
    'cont_var': [1,0,0,1,1,1,1,1],
    'par1':range(8),
    'par2':range(1,9),
    'par3':range(5,13),
    'par4':range(6,14),
}
df = pd.DataFrame(data)

mask = (df['use'] == 1) & (df['cont_var'] != 0)
df['var'] = df[['par1','par2','par3','par4']].sum(axis=1) / df.loc[mask, 'cont_var']

print(df)
   use  cont_var  par1  par2  par3  par4   var
0    1         1     0     1     5     6  12.0
1    1         0     1     2     6     7   NaN
2    1         0     2     3     7     8   NaN
3    1         1     3     4     8     9  24.0
4    1         1     4     5     9    10  28.0
5    0         1     5     6    10    11   NaN
6    0         1     6     7    11    12   NaN
7    0         1     7     8    12    13   NaN
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading