Pandas DataFrame cumsum() method with examples

Spread the love

The cumsum() method in pandas return the cumulative sum over a DataFrame or Series axis. Each cell is populated with the cumulative sum of the values seen so far.

Syntax –

DataFrame.cumsum(axis=None, skipna=True, *args, **kwargs)

Examples –

Let’s create a dataframe to work with.

import pandas as pd

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

1 . Find the cumulative sum of the values seen so far along the index axis –

By default cumsum() method find the cumulative sum of the value seen so far in a column. You can also set it explicitly by setting the axis parameter to axis=0 or index.

df.cumsum(axis=0)

2 . Find the cumulative sum of the values seen so far along the column axis –

To find the cumulative sum of the value seen so far in a row we set the axis parameter to axis=1 or columns.

df.cumsum(axis=1)

Related Posts –

  1. Pandas DataFrame cummax() method with examples
  2. Pandas DataFrame cummin() method with examples
  3. Pandas DataFrame cumprod() method with examples

Rating: 1 out of 5.

Leave a Reply