Home > Back-end >  Sum the values in selected rows of a data frame
Sum the values in selected rows of a data frame

Time:03-04

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:

    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

CodePudding user response:

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
  • Related