You can create a function to perform a sum of columns in a pandas dataframe for a particular interval of dates using the following code:
main.py237 chars7 lines
Here, dataframe
is the pandas dataframe that contains the data, start_date
and end_date
specify the interval for the sum calculation, and cols_to_sum
is a list of columns that we want to sum.
We first create a boolean mask mask
using the start and end dates. We then use this mask to subset the dataframe and select only the rows that are within the specified date range. Finally, we call the sum()
function on the subset to sum up the columns of interest.
You can use this function as shown below:
main.py419 chars11 lines
The output would be:
main.py39 chars4 lines
gistlibby LogSnag