cummax(), cumin(), cumsum(), and cumprod()

The cummax(), cummin(), cumsum(), and cumprod() functions compute the maximum, minimum, sum, and product on a cumulative basis, respectively. Let's understand this by applying the cummax() function on the sample DataFrame:

sales_df.cummax()

The following is the output:

The cummax() function

The skipna parameter in these functions provides control over handling NAs. It is set to True by default, and NAs are excluded. Consider the following DataFrame with NAs to understand the function of this parameter:

sales_df_na

The following is the output:

Sample data with NAs

The cumsum() method can be applied as shown here:

sales_df_na.cumsum()

The following is the output:

The cumsum() function

We can choose to not ignore NAs while doing a cumulative sum by setting skipna to False:

sales_df_na.cumsum(skipna=False)

The following is the output:

The cumulative function with skipna

By default, the aggregation is performed across the row axis since the axis parameter is set to 0 by default. Using the axis parameter, cumulative aggregation can also be performed across columns:

sales_df.cumprod(axis = 1)

The following is the output:

The cumprod() function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset