Pandas DataFrame Cumsum Method

PythonPythonBeginner
Practice Now

Introduction

The Pandas DataFrame cumsum() method is used to calculate the cumulative sum over a DataFrame or Series axis. It returns a DataFrame or Series of the same size that contains the cumulative sum.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") subgraph Lab Skills python/booleans -.-> lab-68606{{"`Pandas DataFrame Cumsum Method`"}} python/lists -.-> lab-68606{{"`Pandas DataFrame Cumsum Method`"}} python/tuples -.-> lab-68606{{"`Pandas DataFrame Cumsum Method`"}} python/dictionaries -.-> lab-68606{{"`Pandas DataFrame Cumsum Method`"}} python/importing_modules -.-> lab-68606{{"`Pandas DataFrame Cumsum Method`"}} python/numerical_computing -.-> lab-68606{{"`Pandas DataFrame Cumsum Method`"}} python/data_analysis -.-> lab-68606{{"`Pandas DataFrame Cumsum Method`"}} end

Import the required libraries

In order to use the cumsum() method, we need to import the pandas library.

import pandas as pd

Create a DataFrame

Next, we need to create a DataFrame using the data we want to perform the cumulative sum on. For example:

df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]})

Calculate the cumulative sum over the index axis

To calculate the cumulative sum over the index axis, we can use the cumsum() method on the DataFrame. By default, the cumsum() method calculates the sum column-wise, so we need to specify the axis as 0.

result = df.cumsum(axis=0)

Calculate the cumulative sum over the column axis

To calculate the cumulative sum over the column axis, we can again use the cumsum() method on the DataFrame, but this time we specify the axis as 1.

result = df.cumsum(axis=1)

Handling null values in the DataFrame

If the DataFrame contains null values, by default the cumsum() method skips these values. However, we can change this behavior and include the null values in the cumulative sum calculation by specifying skipna=False.

result = df.cumsum(axis=0, skipna=False)

Summary

The cumsum() method in Pandas allows us to calculate the cumulative sum over a DataFrame or Series axis. It can be used to perform cumulative sum calculations over both the index and column axes. The method also provides the option to include or exclude null values from the calculation.

Other Python Tutorials you may like