Cumulative statistics
Cumulative statistics can also be helpful in tracking summary statistics over time. In this exercise, you'll calculate the cumulative sum and cumulative max of a department's weekly sales, which will allow you to identify what the total sales were so far as well as what the highest weekly sales were so far.
A DataFrame called sales_1_1
has been created for you, which contains the sales data for department 1 of store 1. pandas
is loaded as pd
.
This exercise is part of the course
Data Manipulation with pandas
Exercise instructions
- Sort the rows of
sales_1_1
by thedate
column in ascending order. - Get the cumulative sum of
weekly_sales
and add it as a new column ofsales_1_1
calledcum_weekly_sales
. - Get the cumulative maximum of
weekly_sales
, and add it as a column calledcum_max_sales
. - Print the
date
,weekly_sales
,cum_weekly_sales
, andcum_max_sales
columns.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Sort sales_1_1 by date
sales_1_1 = ____
# Get the cumulative sum of weekly_sales, add as cum_weekly_sales col
sales_1_1[____] = ____
# Get the cumulative max of weekly_sales, add as cum_max_sales col
____
# See the columns you calculated
print(sales_1_1[["date", "weekly_sales", "cum_weekly_sales", "cum_max_sales"]])