The cumsum() function in Pandas allows you to calculate the cumulative sum over a given axis.
Cumulative sum refers to the total sum of a given data set at a given time. This means that the total sum keeps changing as new data is added or removed.
Let us discuss how to use the cumsum() function in Pandas.
The function syntax is as shown:
The function accepts the following parameters:
- axis – along which axis the cumulative addition is performed. Defaults to zero or columns.
- skipna – allows or disallows null rows or columns.
- **kwargs – Additional keyword arguments.
Function Return Value
The function returns a cumulative sum of a DataFrame along the specified axis.
The example below shows how to use the cumsum() function in Pandas DataFrame.
Suppose we have a sample DataFrame as shown:
To perform the cumulative sum over the columns, we can do the following:
The code above should return:
Note that the values in each column include the total of the previous values.
To operate on the rows, you can set the axis as one. An example is as shown:
This article discussed how to perform a cumulative sum over a specific axis in a Pandas DataFrame using the cumsum() function.
Thanks for reading!!