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

Sum the values in selected rows of a data frame

I have a list of dictionaries:

mylist = [{'Date': '01/02/2020', 'Value': '13'},
{'Date': '01/03/2020', 'Value': '2'},
{'Date': '10/3/2020', 'Value': '4'},
{'Date': '12/25/2020', 'Value': '2'}]

I wanted to sum the Values of the Date from 01/01/2020 to 01/04/2020. I tried the following to select the rows within the date range:

from datetime import datetime
dfmylist = pd.DataFrame(mylist)
dfmylist['Date'] = pd.to_datetime(dfmylist['Date']) 
dfmylistnew = (dfmylist['Date'] > '01/01/2020') & (dfmylist['Date'] <= '01/04/2020')
dfmylistnew1 = dfmylist.loc[dfmylistnew]
dfmylistnew1 

I got the output data frame:

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

    Date    Value
0   2020-01-02  13
1   2020-01-03  2

I want to get the sum Value from the above data frame, which is 15

I tried:

total = dfmylistnew1['Value'].sum()

but the output is 132, instead of 15

>Solution :

From your data, convert values with the right type:

mylist = [{'Date': '01/02/2020', 'Value': '13'},
          {'Date': '01/03/2020', 'Value': '2'},
          {'Date': '10/3/2020', 'Value': '4'},
          {'Date': '12/25/2020', 'Value': '2'}]

df = pd.DataFrame(mylist).astype({'Date': 'datetime64', 'Value': 'int'})
total = df.loc[df['Date'].between('01/01/2020', '01/04/2020', inclusive='right'),
               'Value'].sum()
print(total)

# Output
15
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