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

Conduct the calculation only when the value is not null

I have a data frame dft:

Date              Total Value
02/01/2022          2
03/01/2022          6 
03/08/2022          4
03/11/2022          
03/15/2022          4
05/01/2022          4

I want to calculate the total value in March, I used the following code:

Mar22 = dft.loc[dft['Date'].between('03/01/2022', '03/31/2022', inclusive='both'),'Total Value'].sum()

03/11/2022 has a null value, which caused an error. What should I add to my code so that I only sum the values that are not null?

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

Would that be isnull() == False ?

>Solution :

This issue is that you have an empty string (it should rather be a NaN).

You can ensure having only numbers with pandas.to_numeric:

out = (pd.to_numeric(df['Total Value'], errors='coerce')[dft['Date']
         .between('03/01/2022', '03/31/2022', inclusive='both')].sum()
      )

Or if you only have empty strings as non numeric values:

out = (dft.loc[dft['Date'].between('03/01/2022', '03/31/2022', inclusive='both'), 'Total Value']
          .replace('', float('nan')).sum()
       )

output: 14.0

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